介绍

cpulimit是一款可以对CPU使用率进行限制的软件,既可限制单个特定程序,也可以对整个CPU使用率进行限制,安装使用都很方便,需要的朋友可以试试;

源安装(推荐)
Centos:

 yum install cpulimit

Debian / Ubuntu:

 apt-get install -y cpulimit

编译安装

 cd /tmp
 wget 'https://sunpma.com/other/oss/cpulimit-1.1.tar.gz'
 tar cpulimit-1.1.tar.gz
 cd cpulimit-1.1
 make
 cp cpulimit  /usr/local/sbin/
 rm -rf cpulimit*

使用实例
限制程序名为xmrig的程序仅使用60%的CPU使用率,并在后台一直运行;

 cpulimit -e xmrig -l 60 -b

限制进程号为10086的程序仅使用60%的CPU利用率,并在后台一直运行;

 cpulimit -p 10086 -l 60 -b

限制绝对路径下的软件仅使用60%的CPU利用率,并在后台一直运行;

 cpulimit -e /usr/local/nginx/sbin/nginx -l 60 -b

关闭cpulimit后台进程(取消所有限制)

 kill $(pidof cpulimit)

使用说明
-p --pid=N pid of the process 进程的PID
-e --exe=FILE name of the executable program file 可执行程序文件名
-p --path=PATH absolute path name of the 进程的绝对路径名
-b --background run in background 后台运行
-l --limit=N percentage of cpu allowed from 1 up 允许的CPU百分比,最低为1%
-z --lazy exit if there is no suitable target process 如果目标进程退出或无目标进程则终止
-h --help display this help and exit 显示帮助并退出

注意事项
限制CPU的百分比按照实际的CPU数量而定,比如单核最高为100%,双核最高为200%,三核最高为300%,以此类推即可;

ROOT用户可以限制所有的进程,普通用户只能限制用户有权限管理的进程;