1.摘要
在使用 Linux VPS 时,很多主机商会限制 CPU 的使用率。如果你的 VPS 超过了设定的 CPU 限制,通常会面临被暂停的风险。而如果不加以控制,可能会在高负载的情况下导致 VPS 被暂停,特别是当运行某些资源密集型的进程时。为了避免这个问题,我们可以通过 CPULimit 工具来限制进程的 CPU 使用率,从而确保 VPS 不会因为超负荷使用而遭到暂停。今天就跟大家分享一下如何使用 CPULimit 来限制进程的 CPU 使用,确保 VPS 稳定运行。
2.CPULimit 简介
CPULimit 是一个非常实用的小工具,专门用于限制某个进程的 CPU 使用率。这个工具通过动态调整进程的执行,避免进程使用过多的 CPU 资源。与调整进程优先级(比如 nice 值)不同,CPULimit 是通过控制进程的实际 CPU 使用率,确保进程不会超出你设定的限制。对于那些需要长期运行的进程,CPULimit 能够有效防止它们占用过多的系统资源,影响其他重要任务的执行。
具体来说,CPULimit 会定期向进程发送 SIGSTOP 和 SIGCONT 信号,来控制进程的执行速度,从而确保它们不会占用过多的 CPU 时间。特别适用于那些批处理类作业,需要在后台运行,但又不希望它们占用过多的计算资源。
2.1安装 CPULimit
首先,我们来看看如何在不同的 Linux 发行版上安装 CPULimit:
在 Debian 或 Ubuntu 上安装
在基于 Debian 或 Ubuntu 的系统上,可以通过 apt 命令进行安装:
sudo apt-get install cpulimit在 CentOS、RHEL 或 Fedora 上安装
如果你使用的是 CentOS、RHEL 或 Fedora,可以先启用 EPEL 仓库,然后使用 yum 安装 CPULimit:
sudo yum install epel-release
sudo yum install cpulimit安装完成后,你可以通过运行 cpulimit -h 来查看帮助信息,确保安装成功。
3.使用 CPULimit
CPULimit 使用起来非常简单,以下是常见的命令和用法:
3.1查看帮助信息
如果你对 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
这将显示工具的所有选项,帮助你更好地理解其功能。
3.2限制指定进程的 CPU 使用率
假设你正在运行一个非常占用 CPU 的进程,比如 md5sum(计算文件的 MD5 校验和)。我们可以通过 CPULimit 限制该进程的 CPU 使用率,确保它不会占用过多资源,影响 VPS 的其他操作。
首先,我们运行一个耗费 CPU 的命令(例如 md5sum /dev/urandom):
md5sum /dev/urandom
这个命令会不停地读取 /dev/urandom 生成随机数据并计算其 MD5 校验和,消耗大量 CPU。如果此时你查看 top 命令的输出,你会看到 md5sum 占用了接近 100% 的 CPU。
为了限制该进程的 CPU 使用率到 50%,你可以通过以下命令来实现:
cpulimit --pid 11699 --limit 50
这里,11699 是进程的 PID(可以通过 top 命令查找),--limit 50 表示限制该进程使用不超过 50% 的 CPU。
3.3限制进程名称
除了通过 PID 来限制 CPU 使用率外,你还可以通过进程的名称来限制 CPU 使用。例如,若要限制 md5sum 进程的 CPU 使用率,可以使用:
cpulimit --exe md5sum --limit 50
如果你不想手动查找进程的 PID,可以使用进程的绝对路径来避免名称重复问题:
cpulimit --path /usr/bin/md5sum --limit 50
3.4在启动进程时限制 CPU 使用
如果你知道要执行的命令会占用大量 CPU,可以在启动进程时直接限制 CPU 使用:
cpulimit --limit 50 -- md5sum /dev/urandom
这条命令会启动 md5sum 进程并同时限制它的 CPU 使用率。
3.5终止超出限制的进程
如果你希望在进程的 CPU 使用超过限制时直接终止进程,可以使用 --kill 参数:
cpulimit --pid 11699 --limit 50 --kill
这样,当进程的 CPU 使用率超过 50% 时,CPULimit 会直接终止该进程。
3.6后台运行 CPULimit
默认情况下,CPULimit 会在终端中运行,保持进程监控。如果你希望它在后台运行,可以使用 --background 参数:
cpulimit --pid 11699 --limit 50 --background
3.7自动退出
如果你希望 CPULimit 在没有找到目标进程时自动退出,可以使用 --lazy 参数:
cpulimit --exe md5sum --limit 50 --lazy
4.高级用法
在实际使用中,我们通常会通过 Bash 脚本来管理多个进程的 CPU 使用。比如,当你执行一个命令时,可以直接读取前一个进程的 PID 并限制它的 CPU 使用率:
# 启动进程
md5sum /dev/urandom &
# 获取刚启动进程的 PID
cpulimit --pid $! --limit 50
这样,你就可以避免手动查找 PID,并自动限制进程的 CPU 使用。
5.总结
CPULimit 是一个非常方便且强大的工具,适用于所有 Linux VPS 主机,尤其对于VPS 这种有 CPU 限制的服务商,使用 CPULimit 可以有效避免超出 CPU 限制而被暂停的风险。无论是通过 PID、进程名还是路径限制 CPU 使用,CPULimit 都能提供灵活的解决方案。同时,它还支持后台运行、自动退出等高级功能,帮助你更好地管理系统资源。
通过合理使用 CPULimit,你可以更好地控制进程的资源占用,确保 VPS 的稳定运行,同时避免因 CPU 超限而遭到主机商暂停服务。
评论区