When working on a Linux-based system, multiple processes run in the background, consuming system resources such as CPU usage and time. Generally, an operating system manages such processes automatically, however, sometimes a resource-intensive process may excessively overutilize the CPU. This CPU overutilization can be due to heavy processing or poor development. In such cases, users either directly terminate the process or decrease its CPU usage to a specific limit. In Linux-based systems, users can limit the processor’s CPU usage using a command line utility named CPULimit.

This post illustrates a step by step guide on installing and using the CPULimit tool in Linux to monitor the CPU efficiently.

How to Install CPULimit to Monitor CPU in Linux

To use this utility, first, you need to install it by going through the following steps:

Step 1: Update System Repositories

Before proceeding with the installation of any tool, it’s recommended to update your system. For this purpose, execute the given command from the Linux terminal:

sudo apt update
ubuntu update apt get

Step 2: Install CPULimit

After updating the system, run the following command to install the tool on your Ubuntu machine:

apt install cpulimit

On successful execution of the above command, CPULimit will be successfully installed on your system:

ubuntu install cpulimit command line

Important: To install CPULimit on RHEL, CentOS, or Fedora, first enable the epel repository and then use the below-given commands:

yum install epel-release
yum install cpulimit

Step 3: Verify CPULimit Installation

To verify the installation and see the version number and help screen, run the following command from the terminal:

cpulimit --version

The following snippet verifies the installation of the program on our Ubuntu system:

cpulimit command line monitor cpu ubuntu

Now we have the correct tools to limit how much CPU a process can use on our Linux system.

How to Use CPULimit to Monitor CPU in Linux

Once the CPULimit is successfully installed, you can use it to limit the CPU usage of a process. For this purpose, first, find the Process ID(PID) of the process that you want to limit (for CPU usage). To find/check the Process ID, you can execute the commands like top, htop, or ps. For instance, in the below snippet, we use the top command to find all the processes that consume the CPU time:

top
monitor cpu with top

From the above snippet, you can observe that a process having process ID “6587” is consuming 99% of CPU time. To limit the percentage of CPU time, you can use the CPULimit as follows:

cpulimit -pid <PID> -limit <percent>

In the above syntax, <percent> represents the percentage of CPU usage that you want to limit the process to, and <PID> represents the Process ID. 

Now to limit the CPU usage of a process with PID “6587” to 60%, you must execute the cpulimit command as follows:

cpulimit --pid 6587 --limit 60  

The selected process is detected and now it will not exceed 60% of CPU usage:

Note: When CPULimit is running, it makes the shell non-interactive, meaning it doesn’t expect any user input. Press the keyboard shortcut key “Ctrl + C” to stop the CPU usage limitation operation.

You can use the -b option to run cpulimit in the background. Another option is to use the screen command.

The following table shows different use cases of the cpulimit command:

CommandDescription
cpulimit --pid <ProcessID> --limit <limit> --backgroundUse the --background or -b with the CPULimit command to run it as a background process.
cpulimit --pid <ProcessID> --limit <limit> --killUse the --kill or -k option to kill a process rather than limiting it.
cpulimit --pid <ProcessID> --limit <limit> --cpu <number of CPU cores>Use the --cpu or -c option with the CPULimit command to specify the number of CPU cores present on your system.
cpulimit --pid <ProcessID> --limit <limit> --kill --lazyUse the -z or --lazy option with the CPULimit command to exit(if there is no target process or in case it terminates).
man cpulimit Use this command to learn more about the additional information about cpulimit command and its usage options.

How to Uninstall CPULimit From Linux/Ubuntu

If for some reason CPULimit is no longer needed, you can uninstall it by using the command:

sudo apt remove cpulimit

That’s all about installing and using CPULimit on Linux to monitor the CPU.

Conclusion

CPULimit is a utility tool for Linux-based systems that is used to limit the CPU utilization of a process. It is a great tool to have in the system administrators toolbox.

Links and Resources