我cpulimit
從cron
:
00 16 * * * /usr/bin/cpulimit --limit 20 /bin/sh /media/storage/sqlbackup/backups.sh
當作業開始時,CPU 會像往常一樣出現峰值並發出警報,但沒有發生實際可識別的限制。作業本身會迭代一個包含許多子目錄的目錄,並且rsync
每次執行 's ,我相信這會產生rsync
子進程(運行top 將有一個可用於調用的rsync 的pid,幾分鐘後,它將有一個不同的pid rsync
)。
我不確定如何正確利用cpulimit
來有效限制此過程消耗的使用量。
重要的是要記住這是一個具有 2G RAM 和 1vCPU 的虛擬機器。
答案1
預設cpulimit
不限制子進程,因此根本rsync
不受限制。如果您運行的是足夠新的版本,cpulimit
您應該能夠使用--include-children
(或-i
) 選項。 (也可以看看這個答案.)
$ cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
OPTIONS
-l, --limit=N percentage of cpu allowed from 0 to 400 (required)
-v, --verbose show control statistics
-z, --lazy exit if there is no target process, or if it dies
-i, --include-children limit also the children processes
-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)
Report bugs to <[email protected]>.
這會將您的 cron 條目變更為:
00 16 * * * /usr/bin/cpulimit --include-children --limit 20 /bin/sh /media/storage/sqlbackup/backups.sh
編輯:正如OP(他們自己)回答的那樣,它將適用cpulimit
於rsync
腳本中的命令,但這並不能確保您的腳本在執行其他功能時很好。例如,如果腳本必須處理大量目錄,則可能會使系統陷入困境並導致 CPU 峰值和警報。