Cgsousou 门户 文章 教程/经验 查看内容

Linux下进程 CPU 使用率限制软件 CPULimit 2.4版本安装及其使用方法,附带官方最新版c ...

2023-7-14 18:43| 发布者: lyly| 查看: 218| 评论: 0

摘要: CPUlimit 是一个限制进程的 CPU 使用率的工具(以百分比表示,而不是以 CPU 时间表示)。 当不希望批处理作业占用太多 CPU 时,控制批处理作业很有用。 目标是防止进程运行超过指定的时间比率。它不会更改 nice 值或 ...
前期站长囊中羞涩,一般服务器配置都不怎么高,但是有些进程会特别占用cpu,但是还不能不用,所以今天我们使用 CPULimit这个小软件,他可以对 每个进程的 CPU 使用率进行限制,CPULimit适用于所有的Linux 主机,ubuntu,CentOS、RHEL 或 Fedora 等等都可以,下面进入正题。

为什么我要从新写一个教程呢,因为网上搜了很多,教程多少都有点问题,要不就是cpulimit版本太低,大多都是1.0的,教程也是很老旧,1.0的软件没用的2.0的教程,根本就用不了
软件下载地址:

CPULimit 简介

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

CPULimit 安装

直接下载cpulimit 2.4,地址
上传到服务器根目录(就暂定www/temp),随便哪个目录,因为这个是未编译的,然后直接解压,

命令:
cd www/temp                               (进入目录)
tar cpulimit-2.4.tar.gz                     (解压)
make                                         (编译,不编译无法使用)
cp cpulimit  /usr/local/sbin/            (复制到linux运行加载目录)

以上就安装完成了

然后就可以删除安装文件了

使用示例:

限制程序名为 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)

————————————————
实例

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

复制
cpulimit --pid 11699 --limit 50
执行之后,该进程的 CPU 用量就会被控制在 50% 左右

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

复制
cpulimit --exe ffmpeg --limit 50

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

复制
cpulimit --path /usr/bin/ffmpeg --limit 50

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

复制
cpulimit --limit 50 -- ffmpeg /dev/urandom

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

复制
kill -9 PID


帮助命令如下:

复制
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

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 ffmpge --limit 50 --lazy

4、实用范例

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


路过

雷人

握手

鲜花

鸡蛋

最新评论

关闭

站长推荐上一条 /2 下一条

  • Cgsousou公众号

       关注Cgsousou

  • Cgsousou二维码

       关注Cgsousou

  • Cgsousou

    设计师一站式CG资源库 !!!

  • 安全联盟认证 安全联盟认证