限制 CPU 使用率:

安装 cpulimit

apt install -y cpulimit
mkdir cpu && cd cpu && nano cpu.sh

脚本内容

#!/bin/bash 

while true ; do
 id=`ps -ef | grep cpulimit | grep -v "grep" | awk '{print $10}' | tail -1`
 nid=`ps aux | awk '{ if ( $3 > 95 ) print $2 }' | head -1`
 if [ "${nid}" != "" ] && [ "${nid}" != "${id}" ] ; then
 cpulimit -p ${nid} -l 95 &
 echo "[`date`] CpuLimiter run for ${nid} `ps -ef | grep ${nid} | awk '{print $8}' | head -1`" >> /root/cpu/cpu.log
 fi
 sleep 3
done

赋权

chmod +x /root/cpu/cpu.sh

自启

chmod +x /etc/rc.local;echo "sh /root/cpu/cpu.sh" >> /etc/rc.local