Limit CPU Usage Of A Process on CentOS

Limit CPU Usage Of A Process on CentOS? Cpulimit is a simple program which attempts to limit the cpu usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don’t want them to eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

However, with CloudLinux you may restrict CPU, Memory and other resources. Unfortunately in order to use CloudLinux, you need to pay some money for its license. Let move on with CPULIMIT now.

Installation of CPULIMIT
First, you need to have EPEL repository installed. Type command to install.
$ yum install epel-release

Then, install CPULIMIT
To use you must install it. Type command.
$ yum install cpulimit

CPULimit usage
$ man cpulimit

To limit CPU usage of the process called http to 30%, enter:
$ cpulimit -l 30 httpd
$ cpulimit -e httpd -l 30

To limit CPU usage of the process and it child process called http to 30%, enter:
$ cpulimit -e httpd -i -l 30

Let’s assume the apache2 process ID is 45678, then limit that process to 30% CPU usage as follows:
$ cpulimit -p 45678 -l 30

To find out PID of the process use any of the following:
$ ps aux | less
$ ps aux | grep httpd
$ pgrep -u apache php-cgi
$ pgrep httpd

Instead of using the name of the executable file (-e) of the process ID (-p), we can also pass the absolute path name of the executable program file to cpulimit with the -P switch, the absolute path name of the apache2 executable is /usr/sbin/httpd so we’d use the following:
$ cpulimit -P /usr/sbin/httpd -l 30

You use below scripts to run on startup. Change the number of cpu and process name and it is good to go.


#Set this variable to the number of cores in your processor. This example is for an 8-core CPU. The reason that the number of cores is important is that you need to take it into consideration when setting cpulimit’s -l option. This is explained on the cpulimit project page (see “If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power.”

NUM_CPU_CORES=10 #Change this number to reflect the number of cores in your processor.

cpulimit -e httpd -i -l 100
cpulimit -e mysqld -i -l 100

Have fun ya~

Leave a Reply

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