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 upgrade
Step 2: Then install cpulimit tool by running the below-written command:
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:
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):
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
Example
In the example below, I am limiting the CPU usage for the dd process to 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:
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%:
Run the following command again to analyze the CPU percentage.
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:
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:
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:
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.