Debian/Ubuntu:
apt-get install cpulimit
Centos:
yum install cpulimit
限制所有进程的 CPU 使用率:
#!/bin/bash while true ; do
id=`ps -ef | grep cpulimit | grep -v "grep" | awk '{print $10}' | tail -1`
nid=`ps aux | awk '{ if ( $3 > 75 ) print $2 }' | head -1`
if [ "${nid}" != "" ] && [ "${nid}" != "${id}" ] ; then
cpulimit -p ${nid} -l 85 &
echo "[`date`] CpuLimiter run for ${nid} `ps -ef | grep ${nid} | awk '{print $8}' | head -1`" >> /root/cpulimit-log.log fi
sleep 3done默认情况下 cpulimit 只能对已经存在的进程进行限制,但是设置此脚本为随机自启动即可(设置方法参看上面的脚本链接中),它会对所有进程(包括新建进程)进行监控并限制(3秒检测一次,CPU限制为85%)
保存到 /root/cpulimit.sh,
会自动生成日志文件 /root/cpulimit-log.log
然后将此降本添加开机启动。
设置为开机启动
修改 /etc/rc.local 在对应位置加入 /root/cpulimit.sh 再重启系统,就会全程限制各个进程的 CPU 使用了!