The use of the CPU is one of the main objectives that as users of any system we must keep in mind since this is responsible for the majority of system operations and if it fails or becomes saturated with processes it will undoubtedly cause a negative impact on all the operation of the Linux system and its applications..
CPULimit is a tool that has been developed to limit the Linux CPU usage of a process , based on percentage, not CPU time.
This utility is practical for controlling batch jobs or when it is not desired that too many CPU cycles are consumed, this is achieved by preventing a process from running for more than a specific time reason which is a cause of high CPU consumption without just cause..
It is not the first time we talked about this issue of limiting the use of CPU in Linux systems , since we saw it once with the CPUTool tool that we leave you here so you can take a look, surely its use will also surprise you, fulfilling the objectives you are looking for.
CPULimit does not change the value or priority settings of the settings already established, but the real-time use of the CPU and, in addition to this, there is a possibility that it adapts to the overall system load, dynamically and quickly ..
The control of the amount of CPU used is processed by sending SIGSTOP and SIGCONT POSIX signals to the processes, thus, all processes and secondary threads of the specified process will share the same percentage of CPU.
Let's take action showing with practical examples the installation and use of the CPULimit tool.
1. How to install CPULimit Linux
To install CPULimit we will first update the operating system packages, for this we execute the following command:
sudo apt-get update
Then, we proceed to install CPULimit by running:
sudo apt-get install cpulimit
2. How to use CPULimit Linux
First, we must verify the CPU usage without CPULimit and then we will implement the CPULimit utility for validation. To run an application in a single core, let's run the following:
dd if = / dev / zero of = / dev / null &
Now we will verify the CPU usage with the following command:
top
Normally the use of the CPU reaches 100%, now we can use CPULimit to limit the use of the CPU, for this we will show this process in the foreground using the fg command and cancel it with:
+ C CTRL + C
CPU process command:
fg
With that command you can see the CPU usage.
This proves that CPULimit helps the management of the CPU in an integral way. Now let's run the following:
cpulimit -l 30 dd if = / dev / zero of = / dev / null &
Again we verify the CPU usage with the top command:
We can see that the CPU usage is in a range of 33.1%. To verify the core of your CPU, if you have several, we must use the nproc command:
In this case we only have 1 core.
In case of having more cores, for example 4, we can execute the following line to verify the CPU usage:
for j in `seq 1 4`; do dd if = / dev / zero of = / dev / null & done
In this case we execute the following with a single core:
for j in `seq 1 1`; do dd if = / dev / zero of = / dev / null & done
We can use top again to validate the use of the CPU.
The dd command consumes almost 100% CPU of all available cores, we can eliminate the traces made by dd with the following command:
killall dd
As we can see, thanks to CPULimit we will be able to restrict the CPU usage in Linux, which will help other more important processes to be used properly. Making that the servers do not collapse in performance of the CPU use with certain programs that are not so important for the company, or for the user.