Raspberry Pi

Limit CPU Usage of a Process in Raspberry Pi with CPULimit Tool

In Linux-based systems such as the Raspberry Pi, there are multiple processes that run in the background without the user knowing about them. Most of these processes consume greater CPU resources and reduce the system performance. The only solution left to get rid of these processes is to simply kill them. However, if the users don’t want to kill these processes, they can limit the amount of CPU resources available to processes using the cpulimit command-line utility. It prevents the processes from consuming too much CPU resources on the system, thus helps to improve the overall system performance.

Follow this article to install cpulimit on Raspberry Pi system and limit the CPU usage of a process.

Limit CPU Usage of a Process in Raspberry Pi Using cpulimit

Follow the below-written steps to limit the CPU usage using cpulimit tool:

Step 1: We will install cpulimit tool from the Raspberry Pi repository, so firstly update and upgrade the repository:

$ sudo apt update

$ sudo apt upgrade

Step 2: Then install cpulimit tool by running the below-written command:

$ sudo apt install cpulimit

Step 3: Now let’s run a dd (duplicate data) command so that we can see the CPU usage for it. Basically, the dd command duplicates data from the input file /dev/zero and transfers it to the output file /dev/null again and again. I have chosen this command as an example so that I can show you the maximum CPU usage:

$ dd if=/dev/zero of=/dev/null &

Step 4: Then run the below-mentioned command, as a result, a list of background processes/commands and their CPU usage percentage will be displayed along with their PID (Process Identification Number):

$ top

In the image below you can see that the highest percentage of CPU usage is by dd command:

Step 5: Now to limit the cpu usage to a desired percentage use cpulimit command along with the pid number of the command and a limit to which you want to decrease it:

Syntax

$ sudo cpulimit --pid <pid number> --limit <desired-percentage-out-of-100>

Example

In the example below, I am limiting the CPU usage for the dd process to 50%.

$ sudo cpulimit --pid 9263 --limit 50

Note: PID is a process identification number it could vary, so in the above command always use the PID number which you have found out through the top command:

Step 6: Now let’s verify by running the top command again:

$ top

As an output you will see that the CPU percentage has decreased from 100%, and now it will vary between 51-56%.

In a similar way, you can decrease the CPU usage for a process to different numbers. Usually, it is done for the processes that are utilizing 100% CPU. Let’s now try to decrease it even more like to 15%:

$ sudo cpulimit --pid 9263 --limit 15

Run the following command again to analyze the CPU percentage.

$ top

From the output, you can see that the CPU percentage has decreased to 17.9% it can vary between 17-22%.

Running CPU Limit as a Background Process

The cpulimit tool can run long to limit the process so if you want to keep the terminal free, you can run the CPU limit as a background process by just adding background with the same cpulimit command:

$ sudo cpulimit --pid 9263 --limit 20 --background

Killing the Process Using CPU and PID

If you want to end a certain process running in the background, you can kill it and stop it’s working by running the below-written command along with the PID number of the process:

$ sudo cpulimit --pid <PID number> --limit 20 -k

Accessing CPU Limit Tool Manual

Although we have discussed the usage of cpulimit tool in detail but if you still need help with any other command of cpulimit tool, then run the below-written command to access the manual of it:

$ man cpulimit

As an output the manual will appear on the screen from which you can get help.

Conclusion

To limit CPU usage of a process in Raspberry Pi with the users can install cpulimit tool from the official repository of Raspberry Pi. Then by running the top command to find out the process with the most CPU usage and limit its usage using the cpulimit command along with the command’s PID number. The CPU usage of the process can be set to a desired percentage according to users’ choice.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.