一個(gè)限制進(jìn)程 CPU 使用率的解決方案

一個(gè)限制進(jìn)程 CPU 使用率的解決方案

一 背景

在最近的一個(gè)項(xiàng)目中,需要限制 CPU 使用率。通過查閱各種資料,發(fā)現(xiàn)已經(jīng)有直接可以使用的軟件可以使用,這個(gè)軟件就是cpulimit,這個(gè)軟件使用非常簡單。但是,結(jié)合實(shí)際使用場景,被限制的進(jìn)程不能后臺運(yùn)行,一旦后臺運(yùn)行,進(jìn)程即會立刻退出,而且該進(jìn)程運(yùn)行一段時(shí)間后會產(chǎn)生子進(jìn)程及相關(guān)進(jìn)程。針對這種情況,經(jīng)過思考,通過以下解決方案解決該問題。

二 解決步驟

2.1 安裝cpulimit

[root@gysl-dev ~]# yum -y install epel-release
[root@gysl-dev ~]# yum -y install cpulimit

2.2 執(zhí)行腳本

[root@gysl-dev ~]# sh cpulimit.sh

cpulimit.sh腳本內(nèi)容:

#!/bin/bash
while true;  
    do  
        sleep 30;
        pgrep palrun>&/dev/null;  
        if [ $? -eq 0 ]; then  
            for pid in `pgrep palrun`;  
                do  
                    cpulimit -l 70 -p $pid &  
                done;  
        break;
        fi;  
        done &

將以上腳本加入到需要限制 CPU 使用率的進(jìn)行啟動腳本的最前面,對該腳本的解釋。

由于需要限制 CPU 使用率的進(jìn)程不能在后臺運(yùn)行,所以把限制腳本加入到啟動腳本的最前面,并切換到后臺運(yùn)行,sleep 30秒,待需要限制的進(jìn)程啟動并創(chuàng)建子進(jìn)程后對其進(jìn)行限制。

三 總結(jié)

3.1 官方幫助信息

[root@gysl-dev ~]# cpulimit --help
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 100 (required) #限制 CPU 使用百分比
      -v, --verbose          show control statistics #顯示控制統(tǒng)計(jì)
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes #同時(shí)也限制子進(jìn)程
      -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)

3.2 cpulimit命令使用實(shí)踐

[root@gysl-dev ~]# cpulimit -l 70 -v -p 6258
1 cpu detected
Process 6258 found
Priority changed to -10
Members in the process group owned by 6258: 1

%CPU    work quantum    sleep quantum   active rate
70.09%   73424 us        26575 us       73.42%
69.86%   70778 us        29221 us       70.78%
69.94%   71703 us        28296 us       71.70%
69.77%   70495 us        29504 us       70.50%
69.91%   74194 us        25805 us       74.19%
69.49%   69281 us        30718 us       69.28%
69.78%   72668 us        27331 us       72.67%
70.35%   70634 us        29365 us       70.63%
69.66%   72786 us        27213 us       72.79%
70.27%   69679 us        30320 us       69.68%
69.56%   72325 us        27674 us       72.33%
70.40%   71926 us        28073 us       71.93%
69.43%   71330 us        28669 us       71.33%
69.50%   72184 us        27815 us       72.18%
70.16%   69835 us        30164 us       69.84%
69.37%   74080 us        25919 us       74.08%
69.84%   69417 us        30582 us       69.42%
69.95%   71415 us        28584 us       71.42%
70.81%   71334 us        28665 us       71.33%
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容