CPULimit Limit the CPU usage

cpulimit is a simple program which attempts to limit the CPU usage of a Linux process in percentage. It monitors the CPU usage of a specified process as a daemon, and adjusts its CPU utilization dynamically.. This is useful to control batch jobs, when you don’t want them to eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

cpulimit should run on every Linux 2.2 or greater. It has been reported by several users that cpulimit works fine even on SMP hardware, but consider that if you have more than one cpu there is a little difference in the meaning of cpu usage

Note that cpulimit does not work with some specific kernel processes such as kcryptd, which just ignore SIGSTOP signal.

cpulimit tool

How to install CPUlimit

To install cpulimit on Ubuntu or Debian:

  • # apt-get install cpulimit

To install cpulimit on Fedora:

  • # yum install cpulimit

To install cpulimit on CentOS or RHEL, first enable EPEL repository on your system, and then run:

  • # yum install cpulimit

Install CPUlimit from source

[root@gopal usr]#cd /usr/local/src/
[root@gopal src]#wget http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz
[root@gopal src]#tar -zxvf cpulimit-1.1.tar.gz
[root@gopal src]# cd cpulimit-1.1
[root@gopal cpulimit-1.1]# make
gcc -o cpulimit cpulimit.c -lrt -Wall -O2
[root@gopal cpulimit-1.1]# cp cpulimit /usr/bin/

[root@gopal ~]# cpulimit -v

Usage: cpulimit TARGET [OPTIONS…]
TARGET must be exactly one of these:
-p, –pid=N        pid of the process
-e, –exe=FILE     name of the executable program file
-P, –path=PATH    absolute path name of the executable program file
OPTIONS
-l, –limit=N      percentage of cpu allowed from 0 to 100 (mandatory)
-v, –verbose      show control statistics
-z, –lazy         exit if there is no suitable target process, or if it dies
-h, –help         display this help and exit

Examples

Limit a process by PID to 40% CPU

[root@gopal usr]# cpulimit -p 1423 -l 40
Process 1423 detected

[root@gopal usr]#cpulimit -e /usr/local/bin/ffmpeg -l 20

Thats all..