很多VPS主機(jī)商都會(huì)限制 CPU 使用率,如果超出限制,VPS主機(jī)會(huì)被暫停。比如非常熱門(mén)的搬瓦工 VPS,我們?cè)谑褂眠^(guò)程中可以使用 CPULimit?對(duì)每個(gè)進(jìn)程的 CPU 使用率進(jìn)行限制,可以避免搬瓦工 VPS 的過(guò)程中因超出 CPU 限制而被暫停。CPULimit適用于所有的Linux VPS主機(jī),因?yàn)榘嵬吖?VPS 比較典型,本文教程就以搬瓦工 VPS 為例。
CPULimit 簡(jiǎn)介
CPUlimit 是一個(gè)限制進(jìn)程的 CPU 使用率的工具(以百分比表示,而不是以 CPU 時(shí)間表示)。 當(dāng)不希望批處理作業(yè)占用太多 CPU 時(shí),控制批處理作業(yè)很有用。 目標(biāo)是防止進(jìn)程運(yùn)行超過(guò)指定的時(shí)間比率。它不會(huì)更改 nice 值或其他調(diào)度優(yōu)先級(jí)設(shè)置,而是更改真實(shí)的 CPU 使用率,而且能夠動(dòng)態(tài)且快速地適應(yīng)整個(gè)系統(tǒng)負(fù)載。 使用的 CPU 數(shù)量的控制是通過(guò)向進(jìn)程發(fā)送 SIGSTOP 和 SIGCONT POSIX 信號(hào)來(lái)完成的。 指定進(jìn)程的所有子進(jìn)程和線程將共享相同百分比的 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 進(jìn)程為例。運(yùn)行以下命令:
md5sum /dev/urandom
這是一個(gè)非常耗 CPU 的進(jìn)程,這行指令會(huì)從?/dev/urandom?讀取隨機(jī)數(shù)據(jù),計(jì)算其 MD5 檢查碼,這個(gè)指令非常耗費(fèi) CPU,而且試算不完的,若要中止此進(jìn)程,請(qǐng)按下 Ctrl + c。
注意:不要長(zhǎng)時(shí)間運(yùn)行此命令,僅教學(xué)使用,不然可能你的搬瓦工 VPS 不知不覺(jué)就被暫停了。
在這行?md5sum?指令執(zhí)行之后,我們可以使用?top?指令查看目前系統(tǒng)的狀況,此時(shí)?md5sum?的 CPU 用量應(yīng)該會(huì)接近 100%,如圖所示。
技術(shù)教程主機(jī)格調(diào)" alt="Linux VPS 主機(jī)使用 CPULimit 來(lái)限制 Linux 進(jìn)程的 CPU 使用率技術(shù)教程主機(jī)格調(diào)" width="1015" height="212" data-cfsrc="http://www.lrracing.com/wp-content/uploads/2024/07/4ab12634843dec8769fb94af0d5c4512.png" />
如果想要讓這個(gè)?md5sum?進(jìn)程不要吃掉太多的 CPU 資源,可以使用?cpulimit?來(lái)限制其 CPU 用量(需先在 top 里查看進(jìn)程的 PID,然后另開(kāi)一個(gè)終端操作):
cpulimit --pid 11699 --limit 50
執(zhí)行之后,該進(jìn)程的 CPU 用量就會(huì)被控制在 50% 左右,如下圖所示(因?yàn)槲抑匦逻\(yùn)行了一下,所以 PID 變了,不然是不會(huì)變的)。
技術(shù)教程主機(jī)格調(diào)" alt="Linux VPS 主機(jī)使用 CPULimit 來(lái)限制 Linux 進(jìn)程的 CPU 使用率1技術(shù)教程主機(jī)格調(diào)" width="1020" height="215" data-cfsrc="http://www.lrracing.com/wp-content/uploads/2024/07/42767057484219bd02a1ed8d3a9ab62f.png" />
另外也可以使用進(jìn)程名稱(chēng)來(lái)限制 CPU 使用量:
cpulimit --exe md5sum --limit 50
或者以絕對(duì)路徑來(lái)限制 CPU 使用量,可以避免不同進(jìn)程有相同進(jìn)程名的問(wèn)題:
cpulimit --path /usr/bin/md5sum --limit 50
如果在進(jìn)程執(zhí)行前就已經(jīng)確定要調(diào)整 CPU 用量,也可以直接以?cpulimit?來(lái)執(zhí)行進(jìn)程,例如:
cpulimit --limit 50 -- md5sum /dev/urandom
最后,如果想要停止剛剛已經(jīng)限制的進(jìn)程,那么需要通過(guò)?top?查找 PID 然后?kill:
kill -9 PID
好像沒(méi)看到更優(yōu)雅的結(jié)束進(jìn)程的方式。
CPULimit 高級(jí)用法
1、后臺(tái)運(yùn)行
cpulimit?在執(zhí)行時(shí)也會(huì)占用一個(gè)終端機(jī),若想讓?cpulimit?在后臺(tái)運(yùn)行,可加上?–background?參數(shù):
cpulimit --pid 21203 --limit 50 --background
2、終止 CPU 用量過(guò)高的進(jìn)程
cpulimit?配合?–limit?參數(shù)可以限制進(jìn)程的 CPU 用量上限值,如果進(jìn)程超過(guò)這個(gè)上限值,預(yù)設(shè)會(huì)調(diào)節(jié) CPU 用量,而如果想要在 CPU 用量過(guò)高時(shí)直接中止進(jìn)程,可以加上 –-kill?參數(shù):
cpulimit --pid 21203 --limit 50 --kill
3、自動(dòng)離開(kāi)
在默認(rèn)的狀況下,cpulimit?在執(zhí)行時(shí)若沒(méi)有發(fā)現(xiàn)指定的進(jìn)程(或是指定的進(jìn)程已經(jīng)中止了),它還是會(huì)持續(xù)等待并監(jiān)控系統(tǒng)的進(jìn)程,只要有發(fā)現(xiàn)符合條件的進(jìn)程,就會(huì)繼續(xù)進(jìn)行 CPU 用量的控制。
若想讓?cpulimit?在找不到目標(biāo)進(jìn)程時(shí)自動(dòng)離開(kāi),可以加上?–lazy?參數(shù):
cpulimit --exe md5sum --limit 50 --lazy
4、實(shí)用范例
在撰寫(xiě) bash 腳本時(shí),我們可以先執(zhí)行一個(gè)進(jìn)程,緊接著從 bash 的?$!?變量讀取出前一個(gè)執(zhí)行進(jìn)程的 PID,這樣就可以不需要手動(dòng)查出進(jìn)程的 PID 了:
# 運(yùn)行進(jìn)程 md5sum /dev/urandom & # 限制上一個(gè)進(jìn)程的 CPU 用量 cpulimit --pid $! --limit 50
以上就是 cpulimit 這個(gè)小工具的一些基礎(chǔ)用法和進(jìn)階用法,希望對(duì)大家有所幫助。
搬瓦工推薦方案
下面的 CN2 套餐,目前已經(jīng)全部下架,就是只能看看了。
| 方案 | 內(nèi)存 | CPU | 硬盤(pán) | 流量/月 | 帶寬 | 機(jī)房 | 價(jià)格 | 購(gòu)買(mǎi) |
|---|---|---|---|---|---|---|---|---|
| CN2(下架) | 1GB | 1核 | 20GB | 1TB | 1Gbps | DC3 CN2 DC8 ZNET |
$49.99/年 | 直達(dá) |
| CN2(下架) | 2GB | 1核 | 40GB | 2TB | 1Gbps | $52.99/半年 $99.99/年 |
直達(dá) | |
| CN2 GIA-E (最推薦) |
1GB | 2核 | 20GB | 1TB | 2.5Gbps | DC6 CN2 GIA-E DC9 CN2 GIA 日本軟銀 JPOS_1 荷蘭 EUNL_9 |
$49.99/季度 $169.99/年 |
直達(dá) |
| CN2 GIA-E | 2GB | 3核 | 40GB | 2TB | 2.5Gbps | $89.99/季度 $299.99/年 |
直達(dá) | |
| HK | 2GB | 2核 | 40GB | 0.5TB | 1Gbps | 中國(guó)香港 CN2 GIA | $89.99/月 $899.99/年 |
直達(dá) |
| HK | 4GB | 4核 | 80GB | 1TB | 1Gbps | $155.99/月 $1559.99/年 |
直達(dá) | |
| TOKYO | 2GB | 2核 | 40GB | 0.5TB | 1.2Gbps | 日本東京 CN2 GIA | $89.99/月 $899.99/年 |
直達(dá) |
| TOKYO | 4GB | 4核 | 80GB | 1TB | 1.2Gbps | $155.99/月 $1559.99/年 |
直達(dá) | |
| 搬瓦工優(yōu)惠碼:BWHCGLUKKB | 2024年最新搬瓦工VPS購(gòu)買(mǎi)圖文教程 | |||||||




特惠-scaled.png)

-scaled.png)
質(zhì)VPS服務(wù)商,助力跨境業(yè)務(wù)-scaled.png)
比服務(wù)器-scaled.png)

算-scaled.png)
服8折起-scaled.png)









