站长视角
用户至上

Linux VPS 主机使用 CPULimit 来限制 Linux 进程的 CPU 使用率

优质广告位招租,欢迎联系站长

搬瓦工 VPS 都会限制 CPU 使用率,如果超出限制,VPS主机会被暂停。可以使用 CPULimit 每个进程的 CPU 使用率进行限制,可以避免搬瓦工 VPS 的过程中因超出 CPU 限制而被暂停。CPULimit适用于所有的Linux VPS主机,因为搬瓦工 VPS 比较典型,以搬瓦工 VPS 为例。

CPULimit 简介

CPUlimit 是一个限制进程的 CPU 使用率的工具(以百分比表示,而不是以 CPU 时间表示)。 当不希望批处理作业占用太多 CPU 时,控制批处理作业很有用。 目标是防止进程运行超过指定的时间比率。它不会更改 nice 值或其他调度优先级设置,而是更改真实的 CPU 使用率,而且能够动态且快速地适应整个系统负载。 使用的 CPU 数量的控制是通过向进程发送 SIGSTOP 和 SIGCONT POSIX 信号来完成的。 指定进程的所有子进程和线程将共享相同百分比的 CPU。

CPULimit 安装

在 Debian 或 Ubuntu 中,使用 apt 命令安装:

sudo apt-get install cpulimit

在 CentOS、RHEL 或 Fedora 中,启用 EPEL后,再以 yum 安装:

sudo yum install cpulimit

或者直接:

sudo yum install epel-release cpulimit

CPULimit 使用教程

帮助命令如下:

root@hk:~# cpulimit -h
CPUlimit version 2.4
Usage: cpulimit TARGET [OPTIONS...] [-- PROGRAM]
   TARGET must be exactly one of these:
      -p, --pid=N        pid of the process
      -e, --exe=FILE     name of the executable program file
                         The -e option only works when
                         cpulimit is run with admin rights.
      -P, --path=PATH    absolute path name of the
                         executable program file
   OPTIONS
      -b  --background   run in background
      -f  --foreground   launch target process in foreground and wait for it to exit
      -c  --cpu=N        override the detection of CPUs on the machine.
      -l, --limit=N      percentage of cpu allowed from 1 up.
                         Usually 1 - 100, but can be higher
                         on multi-core CPUs (mandatory)
      -m, --monitor-forks  Watch children/forks of the target process
      -q, --quiet        run in quiet mode (only print errors).
      -k, --kill         kill processes going over their limit
                         instead of just throttling them.
      -r, --restore      Restore processes after they have
                         been killed. Works with the -k flag.
      -s, --signal=SIG   Send this signal to the watched process when cpulimit exits.
                         Signal should be specificed as a number or 
                         SIGTERM, SIGCONT, SIGSTOP, etc. SIGCONT is the default.
      -v, --verbose      show control statistics
      -z, --lazy         exit if there is no suitable target process,
                         or if it dies
          --             This is the final CPUlimit option. All following
                         options are for another program we will launch.
      -h, --help         display this help and exit

以 md5sum 进程为例。运行以下命令:

md5sum /dev/urandom

这是一个非常耗 CPU 的进程,这行指令会从 /dev/urandom 读取随机数据,计算其 MD5 检查码,这个指令非常耗费 CPU,而且试算不完的,若要中止此进程,请按下 Ctrl + c。

注意:不要长时间运行此命令,仅教学使用,不然可能你的搬瓦工 VPS 不知不觉就被暂停了。

在这行 md5sum 指令执行之后,我们可以使用 top 指令查看目前系统的状况,此时 md5sum 的 CPU 用量应该会接近 100%,如图所示。

%title插图%num

如果想要让这个 md5sum 进程不要吃掉太多的 CPU 资源,可以使用 cpulimit 来限制其 CPU 用量(需先在 top 里查看进程的 PID,然后另开一个终端操作):

cpulimit --pid 11699 --limit 50

执行之后,该进程的 CPU 用量就会被控制在 50% 左右,如下图所示(因为我重新运行了一下,所以 PID 变了,不然是不会变的)。

%title插图%num

另外也可以使用进程名称来限制 CPU 使用量:

cpulimit --exe md5sum --limit 50

或者以绝对路径来限制 CPU 使用量,可以避免不同进程有相同进程名的问题:

cpulimit --path /usr/bin/md5sum --limit 50

如果在进程执行前就已经确定要调整 CPU 用量,也可以直接以 cpulimit 来执行进程,例如:

cpulimit --limit 50 -- md5sum /dev/urandom

最后,如果想要停止刚刚已经限制的进程,那么需要通过 top 查找 PID 然后 kill:

kill -9 PID

好像没看到更优雅的结束进程的方式。

CPULimit 高级用法

1、后台运行

cpulimit 在执行时也会占用一个终端机,若想让 cpulimit 在后台运行,可加上 –background 参数:

cpulimit --pid 21203 --limit 50 --background

2、终止 CPU 用量过高的进程

cpulimit 配合 –limit 参数可以限制进程的 CPU 用量上限值,如果进程超过这个上限值,预设会调节 CPU 用量,而如果想要在 CPU 用量过高时直接中止进程,可以加上 –-kill 参数:

cpulimit --pid 21203 --limit 50 --kill

3、自动离开

在默认的状况下,cpulimit 在执行时若没有发现指定的进程(或是指定的进程已经中止了),它还是会持续等待并监控系统的进程,只要有发现符合条件的进程,就会继续进行 CPU 用量的控制。

若想让 cpulimit 在找不到目标进程时自动离开,可以加上 –lazy 参数:

cpulimit --exe md5sum --limit 50 --lazy

4、实用范例

在撰写 bash 脚本时,我们可以先执行一个进程,紧接着从 bash 的 $! 变量读取出前一个执行进程的 PID,这样就可以不需要手动查出进程的 PID 了:

# 运行进程
md5sum /dev/urandom &

# 限制上一个进程的 CPU 用量
cpulimit --pid $! --limit 50

以上就是 cpulimit 这个小工具的一些基础用法和进阶用法,希望对大家有所帮助。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权, 转载请注明出处。
文章名称:《Linux VPS 主机使用 CPULimit 来限制 Linux 进程的 CPU 使用率》
文章链接:https://cnidc.co/19091.html
【声明】:国外主机测评仅分享信息,不参与任何交易,也非中介,所有内容仅代表个人观点,均不作直接、间接、法定、约定的保证,读者购买风险自担。一旦您访问国外主机测评,即表示您已经知晓并接受了此声明通告。
【关于安全】:任何 IDC商家都有倒闭和跑路的可能,备份永远是最佳选择,服务器也是机器,不勤备份是对自己极不负责的表现,请保持良好的备份习惯。
香港110M(含10M CN2)大带宽独服限量促销,香港E3-8G-1T硬盘-3IP,月付仅799!

登录

找回密码

注册