CPULimit, limits the use that a process makes of the CPU

about CPULimit

In the next article we are going to take a look at CPULimit. This is a command line tool that limits CPU usage by a process (expressed in percentage, not CPU time). This will be useful for controlling batch jobs, when we do not want a process to consume too many CPU cycles.

With the use of this tool we are not going to change the value or other priority settings, but the actual use of the CPU. In addition, it is able to adapt to the general load of the system, dynamically and quickly. The control of the amount of CPU used is done by sending señales NEXTSTOP y NEXTCONT POSIX to processes. All child processes and threads of the specified process will share the same CPU percentage.

Install CPULimit

CPULimit is available in most of the default repositories of Unix-like distributions. We can install it using the default package managers in the respective Gnu / Linux distribution. For the example at hand, we will see how to install it on Debian, Ubuntu and Linux Mint. We will only have to open a terminal (Ctrl + Alt + T) and write the following command in it:

sudo apt-get install cpulimit

Whoever wants to can consult other types of facilities in the project GitHub page.

Using CPULimit

Once the tool is installed, it's time to see how it works. To do this, we are going to run a program that consumes a lot of CPU resources. The following commands must be run as the root user.

Creating a script that consumes CPU resources

First we are going to create a file called derrochecpu.sh. I'm going to use the Vim editor, but that each one uses the one they prefer. From the terminal (Ctrl + Alt + T) we will have to write:

vim derrochecpu.sh

Once open, we will press the 'keyI' and then 'i'. Now we are going to add the following lines:

vim script splurgecpu

#!/bin/bash
while :; do :; done;

With this done, it's time to save and exit. To do this we will press the 'keyI'and we will write : Wq to save and close the file. This short script will repeat seamlessly consuming maximum CPU usage. Therefore, it is recommended to test it in a virtual machine.

Now we are going to make this file executable. To do this, from the same terminal (Ctrl + Alt + T) we will execute:

chmod +x derrochecpu.sh

Launching the script

Now we will launch the process in the background. We will do this using the command:

./derrochecpu.sh &

PID script splurgeCPU

We are going to keep the PID of the process. In this case, 6472 is the PID of the launched process.

Checking how much CPU it consumes

We can see the amount of CPU that the process that we just launched consumes, using the command «top» in the same terminal:

top script splurgeCPU

top

As can be seen in the screenshot above, the wastecpu.sh process consumes more than 96% CPU usage. Since it consumes a lot of CPU usage, other tasks are difficult to execute. After a few minutes, the system could crash or freeze. This is where CPULimt comes to our aid.

Limiting CPU usage by PID

Now, let's limit the CPU usage of this process using the CPULimit tool. We are going to limit CPU usage to 35% by means of its corresponding PID (approximately). To do so, run:

cpulimit -l 35 -p 6472 &
  • The option "-l 35»Limits the process to approximately 35%.
  • «-p 6472»Is the PID of derrochecpu.sh that we have seen before.

Checking the effect of CPULimit

Once the previous command has been launched, let's check the CPU usage of the process again. For this we will use the top command again:

top script CPULimit squandering

top

As you can see in the image above, the CPU usage of wastefulcpu.sh has dropped to 35,6%, which is very close to 35%. Now ya we can have more CPU resources to run other processes.

Limiting CPU usage by filename

We have seen how to limit a process using the PID. As well we can execute the CPULimit command specifying the name of the executable program file.

For example, the same example above would be:

cpulimit -l 30 ./derrochecpu.sh &

CPULimit can be useful while running a process that is consuming too much CPU usage. The next time we notice that a program consumes too much CPU, we will simply have to find the PID of the process using the command «top«. When you have it, you just have to limit your CPU usage to a minimum value using the CPULimit command as described in this article.

Uninstall CPULimit

Removing this tool from our system is as simple as opening a terminal (Ctrl + Alt + T) and typing in it:

sudo apt remove cpulimit

What this article has described it's just an example. Obviously, no one in their right mind will launch a script like the one described here on their own computer.


A comment, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   javp said

    Hello:
    It is perfect for an old PC that I have with an amd64 x2 that seems to have a cooling problem and when a process consumes a lot of cpu for several minutes, it heats up to 100º C and shuts down.
    Thus, when I see that a process (generally some websites or video rendering programs) is making me raise the temperature of the cpu, I will use cpulimit to remove "power" from that process.
    Thank you