631 字
3 分钟

CPUlimit 有效防止 Linux 系统 CPU 过载

最近使用Nginx服务器测试 pagespeed 的加速效果时,发现这货会造成 CPU 占用过高,Nginx 经常 100%,尽管使用的是静态缓存,总是触发主机商的CPU占用上限,不停的被强制关机。发现 CPULimit 这款工具刚好可以解决这个问题,便在此记录一下 CentOS 6 下 CPULimit 的安装和使用过程。其实也可以使用 nice 管理进程的优先级也能达到类似的效果。

CPUlimit有效防止Linux系统CPU过载.jpg

项目地址 https://github.com/opsengine/cpulimit

参数介绍#

先了解一下 cpulimit 的参数:

[root@bruce ~]# cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
OPTIONS
-l, --limit=N percentage of cpu allowed from 0 to 200 (required) //cpu限制的百分比
-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) //进程的pid
-e, --exe=FILE name of the executable program file or path name //可执行程序或进程路径
COMMAND [ARGS] run this command and limit it (implies -z) //限制某一条命令执行的cup占用

开始安装#

Linux / OS X 系统,执行:

cd /tmp
wget https://github.com/opsengine/cpulimit/archive/v0.2.zip
unzip v0.2.zip
cd cpulimit-0.2
make
cp src/cpulimit /usr/bin

安装完成!

使用方法#

1、根据进程ID限制

cpulimit -p 1234 -l 40

指限制 pid 为1234的进程,最高只能使用40%的cpu用率。

2、根据进程路径限制

cpulimit -e /usr/local/nginx-1.4.1/sbin/nginx -l 50

指限制 nginx 程序最高只能使用50%的cpu用率。

3、限制某一条命令执行的cup占用

cpulimit -l 60 unzip 5555.zip

指限制解压 5555.zip 这个进程的cpu占用上限值为 60%

注意事项#

  • -l 后面限制的cpu使用量,要根据实际的核心数量而成倍减少。比如上文中的进程1234,40%的限制生效在1核服务器中,如果是双核服务器,则应该限制到20%,四核服务器限制到10%以此类推。
  • root 用户可以限制所有的进程,普通用户只能限制自己有权限管理的进程。
  • 通过某一条命令执行限制cup占用的,请注意 cpulimit 只针对单个进程进行限制。若命令包含两个进程则无效。

最后#

除了 cpulimit 其实我们还可以通过 nice 命令工具实现合理化使用系统资源的目的。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
CPUlimit 有效防止 Linux 系统 CPU 过载
https://blog.moewah.com/posts/9/
作者
GoWah
发布于
2018-09-21
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
GoWah
Hello, I'm GoWah.
分类
标签
站点统计
文章
160
分类
9
标签
350
总字数
301,106
运行时长
0
最后活动
0 天前

目录