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 使用率》
文章鏈接:http://www.lrracing.com/19091.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。