跳转到内容
KN郑某某

限制CPU使用率

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

备忘 1 分钟阅读

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

::: code-group

Terminal window
$ dnf install cpulimit
Terminal window
$ apt install cpulimit

:::

Terminal window
$ cpulimit -h
Terminal window
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]>.

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

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

Terminal window
$ cpulimit -l 320 -p 15855

其中 320 为 400 * 80%

评论