当前位置:网站首页 / Linux / 正文

Debian下利用cpulimit限制CPU使用率

时间:2016年09月09日 | 作者 : liuhui | 分类 : Linux | 浏览: 8826次 | 评论 0

阿里云限量代金券 | 此广告位出租25元/月

对于一些超售比较厉害的vps服务商,他们一般会控制CPU的使用率,从而保证用户不滥用vps的资源,提高vps的稳定性以及性能。这其中比较有名的有bandwagonhost搬瓦工、weloveservers等等,至于linode之类的也会发出报警。所以,如果想避免因CPU使用率过高而带来的被停机的问题,我们需要一些软件来限制 CPU 的使用率和 IO。

cpulimit介绍

cpulimit是一个开源的cpu使用限制工具,可以针对某个进程名、pid等来限制cpu使用率。

cpulimit 命令的工作原理是为进程预设一个 cpu 占用率门限,并实时监控进程是否超出此门限,若超出则让该进程暂停运行一段时间。cpulimit 使用 SIGSTOP 和 SIGCONT 这两个信号来控制进程。它不会修改进程的 nice 值,而是通过监控进程的 cpu 占用率来做出动态调整。

官方网址:http://cpulimit.sourceforge.net/

cpulimit安装

1、apt-get安装

执行如下命令:

1
apt-get install cpulimit

2、编译安装

执行如下命令:

1
2
3
4
5
6
7
wget  'http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz' 
tar zxvf cpulimit-1.1.tar.gz
cd cpulimit-1.1
make
cp cpulimit  /usr/local/sbin/
cd ..rm -rf cpulimit*

cpulimit使用说明

1、cpulimit的参数

1
2
3
4
5
6
7
8
9
10
11
12
# cpulimit -hUsage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 100 (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)

2、cpulimit的用法

1)限制firefox使用30% cpu 利用率

1
cpulimit -e firefox -l 30

2)限制进程号1313的程序使用30% cpu 利用率

1
cpulimit -p 1313 -l 30

3)限制绝对路径下该软件的 cpu 利用率

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

注意事项

1、-l后面限制的cpu使用量,要根据实际的核心数量而成倍减少。比如上文中的进程1234,40%的限制生效在1核服务器中,如果是双核服务器,则应该限制到20%,四核服务器限制到10%以此类推。

2、root用户可以限制所有的进程,普通用户只能限制自己有权限管理的进程。

参考文章:

利用 cpulimit 实现小内存 VPS 防 suspend


本文出自 TENNFY WU,转载时请注明出处及相应链接。

本文永久链接: http://www.tennfy.com/4249.html


推荐您阅读更多有关于“”的文章

猜你喜欢

网站分类
关注电脑博士
友情链接

分享:

支付宝

微信