How we can limit the CPU usage of a sepcified process with the CPUlimit utility?
Cpulimit is a simple program that attempts to limit the CPU usage of a process. It’s expressed in percentage, not in cpu time.
This is useful to control batch jobs, when you don’t want them to eat too much CPU resource. 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.
If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.
Cpulimit should run at least with the same user running the controlled process. It’s much better if you run cpulimit as root, in order to have a higher priority and a more precise control.
Installation of CPUlimit
CPUlimit is available for Debian and Ubuntu, so it can be installed by “aptitude install cpulimit“. If you’re running CentOS or other system, you might need to compile it from source code by yourself.
After the installation, let’s take a look at the cpulimit man page to learn how to use it:
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
Usage of CPUlimit
These examples are from CPUlimit man page as well:
Assuming you have started "foo --bar" and you find out with top(1) or ps(1) that this process uses all your CPU time you can either # cpulimit -e foo -l 50 limits the CPU usage of the process by acting on the executable program file. Note: the argument "--bar" is omitted. # cpulimit -p 1234 -l 50 limits the CPU usage of the process by acting on its PID, as shown by ps(1) # cpulimit -P /usr/bin/foo -l 50 same as -e but uses the absolute path name
Now let’s assume that we want to limit the process httpd to 45%. This is how we do it:
cpulimit -e httpd -l 45
The -e switch takes the name of the executable program file. You can take that name from the output of the top command.
Instead of using the name of the executable program file, we can use the process ID with the -p switch. You can find out the process ID of the httpd process as follows:
ps aux | grep httpd
Let’s assume the httpd process ID is 13500; we can then limit that process to 45% CPU usage as follows:
cpulimit -p 13500 -l 45
Instead of using the name of the executable file (-e) of the process ID (-p), we can also pass the absolute path name of the executable program file to cpulimit with the -P switch.
The absolute path name of the httpd executable is /usr/sbin/httpd so we’d use the following command:
cpulimit -P /usr/sbin/httpd -l 45
Please note that cpulimit will run in the foreground of your terminal until you terminate it with CTRL+C – terminating it will also remove any CPU limits.
Further Readings
CPUlimit Homepage: http://cpulimit.sourceforge.net/
AND Homepage: http://and.sourceforge.net/
Related posts:











NIce post, thank you, Joe! Abcuser also created a great HOWTO for Debian/Ubuntu users here: http://ubuntuforums.org/showthread.php?t=992706
I love the new feature!
Thanks soooooooooooooooooo much for the article, This is just what I needed to read
@Tabitha You’re welcome, I’m very glad to hear that it’s helpful