【www.bbyears.com--Ubuntu】
为防止服务器崩溃限制CPU的使用也很重要。这是你可能会运行在一个crontab的任何自定义脚本特别有用。首先,我们会旋转起来一个Ubuntu的12.10 x64的液滴:
安装cpulimit
apt-get -y install cpulimit
Usage Syntax
Usage: cpulimit TARGET [OPTIONS...]
TARGET must be exactly one of these:
-p, --pid=N pid of the process
-e, --exe=FILE name of the executable program file
-P, --path=PATH absolute path name of the executable program file
OPTIONS
-b --background run in background
-l, --limit=N percentage of cpu allowed from 0 to 100 (mandatory)
-v, --verbose show control statistics
-z, --lazy exit if there is no suitable target process, or if it dies
-h, --help display this help and exit
基准测试CPU使用率
让我们的基准测试CPU使用率而不请将CPULimit。
下面是如何利用你的CPU有一个应用程序的示例:
md5sum /dev/zero &
这个"forks"md5sum然后加工成的背景。
现在你可以看到CPU使用率在顶部:
正如你可以看到它消耗的CPU资源几乎100%(因为我们在这滴一个CPU核心)。
我们可以把这个过程给前台使用FG和CTRL + C取消它:
限制CPU使用率与请将CPULimit
现在我们可以测试请将CPULimit,看看它实际上做什么它应该是。
让我们限制CPU使用率到40%,并运行相同的命令:
cpulimit -l 40 md5sum /dev/zero &
果然,这是限制为40%:
多核液滴
在具有多个处理器的液滴,则需要限制每个进程的CPU使用率。
这里是叉4的进程不受任何限制的脚本,并让他们在您的服务器上同时运行:
for j in `seq 1 4`; do md5sum /dev/zero & done
每个CPU核心是资源利用几乎达到100%:
top - 23:29:28 up 7 days, 13:54, 1 user, load average: 0.80, 1.08, 0.53
Tasks: 77 total, 5 running, 72 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 93.2 us, 6.8 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 95.0 us, 5.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 98.3 us, 1.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 8178228 total, 380196 used, 7798032 free, 28136 buffers
KiB Swap: 0 total, 0 used, 0 free, 251708 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8400 root 20 0 7172 612 520 R 101.7 0.0 0:03.10 md5sum 8401 root 20 0 7172 612 520 R 101.7 0.0 0:03.10 md5sum 8399 root 20 0 7172 616 520 R 98.4 0.0 0:03.06 md5sum 8402 root 20 0 7172 612 520 R 98.4 0.0 0:03.09 md5sum
要使用请将CPULimit为每个进程,将其放置在命令前:
for j in `seq 1 4`; do cpulimit -l 40 md5sum /dev/zero & done
现在每个进程使用的每个线程最多40%,并且不超载的服务器:
top - 23:31:03 up 7 days, 13:55, 1 user, load average: 2.68, 1.72, 0.82
Tasks: 81 total, 5 running, 76 sleeping, 0 stopped, 0 zombie
%Cpu0 : 39.4 us, 0.7 sy, 0.0 ni, 59.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.3 st
%Cpu1 : 38.7 us, 1.7 sy, 0.0 ni, 59.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 39.4 us, 1.3 sy, 0.0 ni, 59.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 39.4 us, 1.7 sy, 0.0 ni, 58.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 8178228 total, 380452 used, 7797776 free, 28144 buffers
KiB Swap: 0 total, 0 used, 0 free, 251708 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8442 root 20 0 7172 616 520 R 40.4 0.0 0:06.10 md5sum 8440 root 20 0 7172 612 520 R 40.0 0.0 0:06.09 md5sum 8435 root 20 0 7172 616 520 R 39.7 0.0 0:06.09 md5sum
8436 root 20 0 7172 612 520 R 39.7 0.0 0:06.10 md5sum
限制firefox使用30% cpu利用率
# cpulimit -e firefox -l 30限制进程号1313的程序使用30% cpu利用率
# cpulimit -p 1313 -l 30根据进程名找出pid的方法
# ps aux | less
# ps aux | grep firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd 可以使用绝对路径限制进程的cpu利用率
# cpulimit -P /opt/firefox/firebox -l 30单核cpu和多核cpu
单核cpu的使用率范围为0%-100%,四核cpu的使用率范围为0%-400%.