Limit CPU Usage Per Process in Linux

You can use “cpulimit” program that can limit the CPU usage for a particular process. Follow the process below to set a limit for CPU usage.

Install cpulimit

Type the following commands to install “cpulimit”:

# cd /tmp
# wget ‘http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz’
# tar -zxvf cpulimit-1.1.tar.gz
# cd cpulimit-1.1
# make
# cp cpulimit /usr/local/sbin/
# rm -rf cpulimit*

Debian / Ubuntu Linux users

Type the following command to install “cpulimit”:

$ sudo apt-get update
$ sudo apt-get install cpulimit

How do I use cpulimit?

To limit CPU usage of the process called firefox to 30%, enter:

# cpulimit -e gzip -l 30

To limit CPU usage of the process to 30% by using its PID, enter:

# cpulimit -p 1313 -l 30

To find out PID of the process use any of the following:

# ps aux | less
# ps aux | grep gzip
# pgrep -u php php-cgi
# pgrep gzip

You can also use absolute path name of the executable, enter:

# cpulimit -P /bin/gzip -l 30

Parameter:

  • -p : Process PID.
  • -e : Process name.
  • -l : percentage of CPU allowed from 0 to 100.
  • -P: absolute path name of the executable program file.

Linux command -top

If you are a linux user then I am sure you are familiar with “top” command. Have you any clear idea about us, sy, ni, wa, hi, si? Open your terminal and enter “top” then see the 3rd line “Cpu(s):”

1. us -> User CPU time: The time the CPU has spent running users’ processes that are not niced.
2. sy -> System CPU time: The time the CPU has spent running the kernel and its processes.
3. ni -> Nice CPU time: The time the CPU has spent running users’ proccess that have been niced.
4. wa -> iowait: Amount of time the CPU has been waiting for I/O to complete.
5. hi -> Hardware IRQ: The amount of time the CPU has been servicing hardware interrupts.
6. si -> Software Interrupts.: The amount of time the CPU has been servicing software interrupts.