Cover image for Limiting CPU Usage Using CPULimit

Teddy Zugana

使用 CPULimit 限制 Linux 程序的 CPU 使用量。在本教學中,我將分享一點關於如何使用 cpulimit 來限制程序的 CPU 使用量。我自己經常在程序消耗不合理 CPU 時使用這個工具。我曾經遇過在使用 Burp Suite 時,筆記型電腦的 CPU 使用率飆升至 100%。有了 cpulimit 這樣的工具,當然非常有幫助。

安裝 CPULimit
安裝 CPULimit 相當簡單。Debian 和 Ubuntu 用戶及其衍生版本只需執行以下指令:
Bash

sudo apt install -y cpulimit

Enter fullscreen mode Exit fullscreen mode

如果您是 RHEL 或 CentOS 用戶,請使用 yum:
Bash

sudo yum install cpulimit

Enter fullscreen mode Exit fullscreen mode

請確保您已在 RHEL/CentOS 系統中新增 EPEL 儲存庫。

使用 CPULimit
在執行 cpulimit 之前,透過 htop 監控 CPU 使用量。

這裡我想限制 Burp Suite 程序,所以我們使用 grep 進行過濾:
Bash

ps -aux | grep -i burp

Enter fullscreen mode Exit fullscreen mode

找到目標程序的 PID 後,使用以下指令執行 cpulimit:
Bash

sudo cpulimit --pid 150287 --limit 40

Enter fullscreen mode Exit fullscreen mode

請自行調整 PID。這裡我將 Burp 程序的 CPU 使用率限制在 40%,因此使用 --limit 40 參數。您可以根據需求進行調整。

現在再次使用 htop 或 top 檢查資源。

若要讓 cpulimit 在背景執行,請使用 --background 或 -b 參數:
Bash

sudo cpulimit --pid 150287 --limit 40 --background

Enter fullscreen mode Exit fullscreen mode

若要查看其他可用指令,請使用:

Bash

cpulimit -h
man cpulimit

Enter fullscreen mode Exit fullscreen mode