How to limit the CPU usage of a Linux process

If you are a Linux system admin, there are cases where you would like to limit the CPU usage of a certain process such that the process does not eat up more than X% of CPU resource at any time. cpulimit is a command-line Linux program that can do exactly that, i.e., limiting 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.

To install cpulimit on Ubuntu or Debian:

$ sudo apt-get install cpulimit

To install cpulimit on Fedora:

$ sudo yum install cpulimit

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

$ sudo yum install cpulimit

Example usages of cpulimit are shown below:

$ sudo cpulimit -p 8645 -l 10
$ sudo cpulimit -e /usr/local/bin/myprog -l 20

The first command monitors the CPU usage of a process with PID 8645, and limits its CPU usage to 10%. Similarly, the second command limits the CPU usage of /usr/local/bin/myprog to 20%.

The cpulimit program runs as a user-space daemon which sends SIGSTOP and SIGCONT signals to a specified Linux process to adjust its CPU usage dynamically. Note that cpulimit must be executed either by the root or by the owner of the process being monitored. Conceptually, cpulimit works as follows.

while (1) {
     wait for some time
     send SIGSTOP to the process
     wait for some time
     send SIGCONT to the process
}

Testing with Debian 6.0 indicates that cpulimit is not SMP-aware. That is, specifying "-l X" actually means X% of one CPU. So for example, if you set "-l 100" on a dual-core machine, you are limiting the usage to 50% of available CPU resource.

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

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

3 thoughts on “How to limit the CPU usage of a Linux process

    • Sorry to hear that. To add to the difficulty, also note that in recent versions of Linux kernel (above 2.6.32), kcryptd process is replaced with generic kworker process. Since a pool of kworker processes deal with all sort of kernel activities besides disk encryption, you can no longer tell which process ID is involved in disk encryption. You will be probably better off rate limiting disk I/O…

  1. That's not true (anymore).

    If you have a dual core, you would limit the cpu usage in a range from 0%-200%.

    If you have a quad core, from 0%-400%, and so on.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *