1) 首先,确认自己的系统版本
# cat /etc/redhat-release CentOS release 6.6 (Final)
# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm
3) 安装cpulimit
# yum install cpulimit
4) 使用cpulimit进行cpu利用率限制
1. 先了解一下cpulimit的参数
# cpulimit -h Usage: 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) Report bugs to .
2. 根据进程ID限值
# cpulimit -p 1234 -l 40
此命令的含义为,限制pid号为1234的进程,最高只能使用40%的cpu。
3. 根据进程路径限制
# cpulimit -e /usr/local/nginx-1.4.1/sbin/nginx -l 50
限制nginx程序只能使用最高50%的cpu。
5) 注意事项
- -l后面限制的cpu使用量,要根据实际的核心数量而成倍减少。比如上文中的进程1234,40%的限制生效在1核服务器中,如果是双核服务器,则应该限制到20%,四核服务器限制到10%以此类推。
- root用户可以限制所有的进程,普通用户只能限制自己有权限管理的进程。