Skip to content

Linux 中使用 cpulimit 限制进程在每单位时间内可以分配的 CPU 周期的比例

安装

shell
$ dnf install cpulimit
shell
$ apt install cpulimit

查看说明

shell
$ cpulimit -h
shell
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 400 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <[email protected]m>.

特别注意

-l 在多核系统中,设置比例的时候,最大值不是100,而且 核心数*100

运行

例如4核服务器上有个进程id为15855的进程,我想限制它的CPU使用率为整体的80%,可以执行下面的命令:

shell
$ cpulimit -l 320 -p 15855

其中 320 为 400 * 80%