frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

[Ubuntu/Debian/CentOS/Fedora] How to limit CPU usage for process?

WilliamWilliam Moderator
edited May 30 in Performance and Security

There can be cases, when your server performance can slow down when even single process uses all of your server CPU capability. Such usage will cause issues for other processes to work properly on your VPS instance. Since when one process is using all of the resources, other processes has to wain in queue to start their tasks. This can be solved by limiting CPU resource usage with CPULimit software.

Main purpose of CPULimit is to set restriction on specific process on how much CPU it can use. When limitation is implemented, there are no influence on nice value or the settings on priorities, but on real CPU usage instead.

0. Requirements:
Root or sudo.
nano or other text editor.

1.1 Installation if using Ubuntu/Debian:
sudo apt-get install cpulimit
1.2 Installation if using CentOS/Fedora:

Firstly, it will be required to install EPEL repository if it was not done previously:
sudo yum install epel-release
Afterwards, the installation can be initiated:
sudo yum install cpulimit

or 

sudo dnf install cpulimit
2. Usage of CPULimit:

To check the effectiveness of CPULimit, let's create a process (usecpu.sh) that will consume all of your server's CPU:
sudo nano usecpu.sh
Enter the following code into the usecpu.sh file:
#!/bin/bash
while :; do :; done;
Save the code on your file. This code will create a loop which will consume all of server's CPU. When the file is created, set executable permission for it:
sudo chmod +x usecpu.sh
The created process can be started with using:
./usecpu.sh &
After initiation, you will receive a result of process id, for example 5700:
[1] 1887
If everything is correct, the new process should use all of server's CPU. You can check the usage of your CPU by using top command:
PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND           
 2912 root      20   0   12512    896    792 R 99.9  0.0   0:18.38 usecpu.sh        
 2893 root      20   0   39688   3660   3092 R  0.3  0.2   0:00.02 top              
    1 root      20   0   37912   6000   4004 S  0.0  0.3   0:01.52 systemd          
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd         
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.05 ksoftirqd/0      
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H     
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.10 kworker/u2:0     
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.49 rcu_sched        
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh           
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0      
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 watchdog/0       
   11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs        
   12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns            
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 perf             
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd       
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback        
   16 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd  
As you can see in the table, the usecpu.sh process uses 99.9% of server CPU. Such CPU usage will prevent other processes from working successfully and can finally result in server OS inactivity. This is where CPULimit comes to usage.

If you wish to limit the process to use only 30% of your server total CPU, initiate the following command:
cpulimit -l 30 -p 2912 &
  • * -l 30  will set the limit to the provided number of %. In this case 30%.
  • * -p 2892   is used to identify the process by it's PID, as seen in the first column from top command.

Now, when the process is limited by CPULimit let's check the results again by initiating top command:

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND           
 2912 root      20   0   12512    996    896 T 31.6  0.0   0:29.76 usecpu.sh        
 2915 root       9 -11    8612   1544   1428 S  0.3  0.1   0:00.02 cpulimit         
    1 root      20   0   37912   6000   4004 S  0.0  0.3   0:01.53 systemd          
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd         
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.06 ksoftirqd/0      
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H     
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.11 kworker/u2:0     
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.52 rcu_sched        
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh           
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0      
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.02 watchdog/0       
   11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs        
   12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns            
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 perf             
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd       
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback        
   16 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd 
Now the usecpu.sh process CPU consumption will be limited to 31.6% of CPU (can be +/- 5% deflection).

If you wish to use process name instead of PID for the limitation, you can form your command from such example:
cpulimit -l 25 ./usecpu.sh &
Sign In or Register to comment.

Learn how to install a web and database server, email, FTP client or other applications. Discover and share information on server security or optimization recommendations.
Feel free to join our constantly expanding community, participate in discussions, strengthen your knowledge on Linux and Windows server management!
© 2013 - 2018 Time4VPS. All rights reserved.

Get In Touch