Linux 限制进程占用CPU
2015-08-19 11:55
761 查看
cpulimit is a small program written in C that allows to limit CPU usage by Linux process. Limit is specified in percentage
so it’s possible to prevent high CPU load generated by scripts, programs or processes.
I found cpulimit pretty useful for the scripts running from cron, for example I can do overnight backups and be sure that compression of 50GB file via gzip won’t eat all CPU resources and all other system processes
will have enough CPU time.
In most of Linux distributions cpulimit is available from binary repositories so you can install it using commands:
or
If it’s not possible in your distro then it’s extremely easy to compile it:
From that moment you can run commands limited by CPU percentage, e.g. below command executes gzip compression so that gzip process will never step over 10% of CPU limit:
You can check actual CPU usage by gzip using commands:
or
Btw, the first command contains ‘grep [g]zip’ to avoid the last line in common output:
Using cpulimit you can also allocate CPU limit to already running processes, e.g. below command will allocate 20% CPU limit to process with PID 2342:
It’s possible to specify process by its executable file instead of PID:
http://www.linuxscrew.com/2012/07/03/limit-cpu-usage-of-linux-process/
so it’s possible to prevent high CPU load generated by scripts, programs or processes.
I found cpulimit pretty useful for the scripts running from cron, for example I can do overnight backups and be sure that compression of 50GB file via gzip won’t eat all CPU resources and all other system processes
will have enough CPU time.
In most of Linux distributions cpulimit is available from binary repositories so you can install it using commands:
sudo apt-get install cpulimit |
sudo yum install cpulimit |
cd /usr/src/ wget --no-check-certificate https://github.com/opsengine/cpulimit/tarball/master -O cpulimit.tar tar -xvf cpulimit.tar cd opsengine-cpulimit-9df7758 make ln -s cpulimit /usr/sbin/cpulimit |
/usr/sbin/cpulimit --limit=10 /bin/gzip vzdump-openvz-102-2012_06_26-19_01_11.tar |
ps axu | grep [g]zip |
top |
root 896448 10.0 3.1 159524 3528 ? S 13:12 0:00 /usr/sbin/cpulimit --limit=10 /bin/gzip vzdump-openvz-102-2012_06_26-19_01_11.tar root 26490 0.0 0.0 6364 708 pts/0 S+ 15:24 0:00 grep gzip |
/usr/sbin/cpulimit -p 2342 -l 20 |
/usr/sbin/cpulimit -P /usr/sbin/nginx -l 30 |
相关文章推荐
- Redhat配置使用centos的yum源
- centos安装pip和mitmproxy
- Linux常用命令
- linux下的定时任务
- linux笔记之1--文件系统+创建、删除文件和文件夹命令
- Linux LVS-NAT实例演示
- centos下把python版本升级成2.7
- Linux内核的进程切换(上)
- Linux 环境进程间通信(六) 套接口
- Linux环境进程间通信(五): 共享内存
- Ubuntu Linux 安装 .7z 解压和压缩文件
- linux查看当前ssh登陆的ip
- Linux各版本内核源码下载地址
- Linux编程C++内存管理之内存分配详解
- Linux环境进程间通信(四) 信号灯
- Linux环境进程间通信(三)消息队列
- Linux环境进程间通信(二): 信号
- 深入理解Linux内核-I/O体系结构和设备驱动程序
- linux下的Source命令的基本功能
- Linux 内存查看