Limit CPU Usage of a Process in Linux with CPULimit Tool

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
Monitor CPU Usage in Linux
Monitor CPU Usage in Linux

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).

Limit CPU Usage of Process in Linux
Limit CPU Usage of Process in Linux

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
Throttle CPU Usage in Linux
Throttle CPU Usage in Linux

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.

  1. Find Top Running Processes by Highest Memory and CPU Usage in Linux
  2. Cpustat – Monitors CPU Utilization by Running Processes in Linux
  3. CoreFreq – A Powerful CPU Monitoring Tool for Linux Systems
  4. Find Top Running Processes by Highest Memory and CPU Usage in Linux
  5. 20 Command Line Tools to Monitor Linux Performance
  6. 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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 Comments

Leave a Reply
  1. 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.

    Reply
  2. 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.

    Reply
    • @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.

      Reply

Got Something to Say? Join the Discussion...