In an earlier post, we’ve explained CPUTool for limiting and controlling CPU utilization of any process in Linux. It allows a system administrator to interrupt execution of a process (or process group) if the CPU/system load goes beyond a defined threshold. Here, we will learn how to use a similar tool called cpulimit.
Cpulimit is used to restrict the CPU usage of a process in the same way as CPUTool, however, it offers more usage options compared to its counterpart. One important difference is that cpulimit doesn’t manage system load unlike cputool.
Suggested Read: 9 Useful Commands to Get CPU Information on Linux
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.
# yum install epel-release
# yum install cpulimit
Limiting Process CPU Usage With CUPLimit
In this sub section, we’ll explain how cpulimit works. First, let’s run a command (same dd command we looked at while covering cputool) which should result into a high CPU percentage, in the background (note that the process PID is printed out after running the command).
$ dd if=/dev/zero of=/dev/null & [1] 17918
Next, we can use the top or glances tools which output the actual frequently updated state of a running Linux system, to watch the CPU usage of the command above.
$ top
Looking at the output above, we can see that the dd process is utilizing the highest percentage of CPU time 100.0%.
But 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 dd command (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 dd command with top or glances. From the output, the value ranges from (51.5%-55.0% or slightly beyond).
We can throttle its CPU usage for a second time as follows, this time lowering the percentage further as follows:
$ sudo cpulimit --pid 17918 --limit 20 Process 17918 detected
As we did before, we can run top or glances to view the new CPU usage for the process, which will range from 20%-25.0% or slightly beyond this.
$ top
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
Do check out the following useful guides for finding CPU info and CPU/system performance monitoring.
- Find Top Running Processes by Highest Memory and CPU Usage in Linux
- Cpustat – Monitors CPU Utilization by Running Processes in Linux
- CoreFreq – A Powerful CPU Monitoring Tool for Linux Systems
- Find Top Running Processes by Highest Memory and CPU Usage in Linux
- 20 Command Line Tools to Monitor Linux Performance
- 13 Linux Performance Monitoring Tools – Part 2
In comparison, after testing CPUTool and CPULimit, we noticed that the former offers a more effective and reliable “process CPU usage limitation” functionality.
This is according to the percentage range of CPU usage observed after running both tools against a given process. Try out both tools and add your thoughts to this article using the feedback form below.
Without using cpulimit the process uses 95-100% of the cpu (4 cores / 8 threads).
Using this code:
cpulimit --pid 17918 --limit 75
Makes my cpu use only 8-12%…..
Also tried:
cpulimit --pid 17918 --limit 75 -cpu 4 and --cpu 8
Same result… the cpulimit module seems glitched.
Sad that Linux is so shit in 2019 since Windows could have controlled the CPU usage 100% precise since like 2004 and that from the task manager without installing additional shit and waste more time.
@Dragon
Thanks for sharing your experience with us. We will investigate more based on your response.
--background
or-b
is no longer a valid option.@The TechReader
Many thanks for mentioning this important point.
You are correct. But how can I use background process limitations? Is there any new command introduced?
such tool is a bit outdated for these times, since long time ago, Linux allows controlling these kind of resources via cgroups, in a much more way than using SIGSTOP/SIGCONT. Indeed in current times, you have systemd-run to do these kinds of things and much more.
@gera
Yap, that’s true. Thanks for pointing this out, we’ll soon organize an article about cgroups and resource control especially under systemd. However, these tools are still useful even in current Linux systems.