When you're working on a Linux system, numerous processes run in the background. These processes take up system resources in the form of CPU usage and time.

While in most situations, the OS manages these processes automatically, sometimes a resource-intensive process can over utilize the CPU due to heavy processing or poor development. The answer is usually to kill the process directly or limit its CPU usage to a certain limit.

Luckily on Linux, you can limit a process's CPU usage using a command-line utility called cpulimit.

How to Identify a Process With High CPU Usage

Before you can limit the percentage of system resources a process can use, you need to find the process ID of that particular process. A process ID (or PID) is a unique number that your system uses to identify a process.

On Linux, there are several ways to get detailed information related to processes. You can use the top command to get a list of processes currently running on your system.

        top
    

Output:

checking cpu usage with top

The %CPU column shows the percentage of CPU the particular process is using. If your computer is trying to process more data than it can, then some specific process will have a CPU usage of 100%. Check the table to see if there's any process with high CPU usage.

Once you've found the process with high CPU usage, note down its PID. The process ID is important for limiting the usage of the process.

Limit CPU Usage With cpulimit

As mentioned above, cpulimit is a command-line utility that adds a limit to the amount of system resources used by a specific process on your computer. Since most of the Linux distributions do not ship with cpulimit preinstalled, you'll have to install it manually.

You can install the package on Ubuntu and other Debian-based distributions as follows:

        sudo apt install cpulimit
    

On Arch-based distributions like Manjaro Linux:

        sudo pacman -S cpulimit
    

Cpulimit is available on the EPEL (Extra Packages for Enterprise Linux) repository. Therefore, to install it on CentOS and RHEL distributions, you'll have to enable the EPEL repository first.

        yum install epel-release
yum install cpulimit

Basic Syntax

To use cpulimit, you'll have to pass one of the following three arguments with the command:

  1. -p or --pid: The process ID of a process
  2. -e or --exe: The name of the executable file
  3. -P or --path: Absolute path of the executable file

The basic syntax of the command is:

        cpulimit -p pid
cpulimit -e executablename
cpulimit -P /path-to-executable

Limit the CPU Usage of a Process

You can use the --limit or -l flag of the cpulimit utility to add a limit to the resources that a process can use. To force a process with PID 81550 to use only 50% of the CPU:

        sudo cpulimit -p 81550 --limit 50
    

Here, cpulimit will restrict the CPU usage of the process as long as it's running. If you stop the execution of cpulimit, the CPU usage of that specific process will go back to normal.

A great solution to prevent this issue is to run cpulimit in the background. You can add the --background or -b flag with the command to send the command to the background.

        sudo cpulimit -p 81550 --limit 50 --background
    

If the --background option doesn't work, you can add an Ampersand (&) after the command to send it to the background.

        sudo cpulimit -p 81550 --limit 50 &
    

Use the top command to check if the aforementioned command works.

low cpu usage with cpulimit

As you might have noticed, the CPU usage of the dd command went down to 48.8%.

Kill a Process Using Its PID

Instead of limiting the CPU usage, you can completely shut the process down by killing it with the --kill flag.

        sudo cpulimit -p 81550 --limit 50 --kill
    

Smarter Process Management in Linux

Cpulimit is a great utility if you often bump into processes with high CPU usage. GNOME users who aren't comfortable with the command line can also use System Monitor to manage processes on their system.

In addition to using tools like cpulimit, you can also lower the priority on Linux to provide less resources to a specific process. The nice and renice commands are a lifesaver when it comes to managing process priority in Linux.