+5 votes
355 views
How to limit Linux CPU usage with CPULimit

in Linux / Unix by (552k points)
reopened | 355 views

1 Answer

+3 votes
Best answer

1. How to install CPULimit Linux
2. How to use CPULimit Linux

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 
image

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 & 
image

Now we will verify the CPU usage with the following command:

 top 
image

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. image

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 & 
image

Again we verify the CPU usage with the top command:

image

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:

image

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 
image

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 
image

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.


by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked May 3, 2023 in Windows11 by backtothefuture (552k points) | 64 views
+3 votes
1 answer
+3 votes
1 answer
asked Sep 26, 2019 in Linux / Unix by backtothefuture (552k points) | 928 views
+4 votes
1 answer
asked May 7, 2020 in Windows 10 by backtothefuture (552k points) | 606 views
+4 votes
1 answer
asked Nov 4, 2019 in WiFi and ADSL by backtothefuture (552k points) | 269 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users