Cpulimit is used to restrict the CPU usage of a process and offers more usage options compared to other tools. One important difference is that cpulimit doesn’t manage system load unlike cputool.
Install CPULimit to Limit CPU Usage Of a Process in Linux
CPULimit is available to install from default software repositories of Debian/Ubuntu and its derivatives using a package management tool.
$ sudo apt install cpulimit
In RHEL/CentOS and Fedora, you need to first enable EPEL repository and then install cpulimit as shown.
RHEL/CentOS 7 64 Bit
## RHEL/CentOS 7 64-Bit ## # wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm # rpm -ivh epel-release-7-9.noarch.rpm
RHEL/CentOS 6 32-64 Bit
## RHEL/CentOS 6 32-Bit ## # wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm ## RHEL/CentOS 6 64-Bit ## # wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # rpm -ivh epel-release-6-8.noarch.rpm
RHEL/CentOS 5 32-64 Bit
## RHEL/CentOS 5 32-Bit ## # wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm # rpm -ivh epel-release-5-4.noarch.rpm ## RHEL/CentOS 5 64-Bit ## # wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm # rpm -ivh epel-release-5-4.noarch.rpm
RHEL/CentOS 4 32-64 Bit
## RHEL/CentOS 4 32-Bit ## # wget http://download.fedoraproject.org/pub/epel/4/i386/epel-release-4-10.noarch.rpm # rpm -ivh epel-release-4-10.noarch.rpm ## RHEL/CentOS 4 64-Bit ## # wget http://download.fedoraproject.org/pub/epel/4/x86_64/epel-release-4-10.noarch.rpm # rpm -ivh epel-release-4-10.noarch.rpm
# yum install epel-release
# yum install cpulimit
Limiting Process CPU Usage With CUPLimit
Now we can limit this using cputlimit as follows. The --pid
or -p
option is used to specify the PID and --limit
or -l
is used to set a usage percentage for a process. The command below will limit the process PID 17918 to 50% use of one CPU core.
$ sudo cpulimit --pid 17918 --limit 50 Process 17918 detected
Once we run cpulimit, we can view the current CPU usage for the process 17918 with top. From the output, the value ranges from (51.5%-55.0% or slightly beyond).
Note: The shell becomes un-interactive – doesn’t expect any user input when cpulimit is running. To kill it (which should stop the CPU usage limitation operation), press [Ctrl + C]
.
To run cpulimit as a background process, use the --background
or -b
switch, freeing up the terminal.
$ sudo cpulimit --pid 17918 --limit 20 --background
To specify the number of CPU cores present on the system, use the --cpu
or -c
flag (this is normally detected automatically).
$ sudo cpulimit --pid 17918 --limit 20 --cpu 4
Rather than limit a process’s CPU usage, we can kill it with the --kill
or -k
option. The default is signal sent to the process is SIGCONT, but to send a different signal, use the --signal
or -s
flag.
$ sudo cpulimit --pid 17918 --limit 20 --kill
To exit if there is no suitable target process, or in case it dies, include the -z
or --lazy
like this.
$ sudo cpulimit --pid 17918 --limit 20 --kill --lazy
For additional information and usage options, view the cpulimit man page.
$ man cpulimit