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