Linux Limit CPU Usage Per Process

by Vivek Gite · 18 comments

I don't want background process to eat all my CPU. I know how to find out CPU utilization but how do I limit the cpu usage of a process under Linux operating system? How do I force a process to limit itself to 25% only?

You can use cpulimit program that attempts to limit the cpu usage of a process. Limits are expressed in percentage and not in cpu time. cpulimit 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.

Install cpulimit

Type the following commands to install latest stable release:
# 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*

A note about 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 firefox -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 firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd

You can also use absolute path name of the executable, enter:
# cpulimit -P /opt/firefox/firebox -l 30
Where,

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

Root vs Normal User Account

From the project webpage:

cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

A Note About SMP (Multicore / MultiCpu) Systems

Again quoting from the project webpage:

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. In any case, the percentage is the same of what you see when you run top.

Related Throttling Utilities

  1. ionice utility - Avoid sudden outburst of backup shell script / program disk I/O.
  2. Limit disk I/O for rsync tool.
  3. Linux nice command: Run Process With Modified Scheduling Priority ( nicenesses )
  4. renice command: Change the Priority of a Already Running Process

Recommended Readings:

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 18 comments… read them below or add one }

1 sajmon 05.23.09 at 8:21 pm

does cpulimit runs firefox in your example, like nice?

Or can it control cpu usage of already working programs?

2 Vivek Gite 05.23.09 at 8:27 pm

Or can it control cpu usage of already working programs?

Firefox is already running and icpulimit going to control its cpu usage.

3 Someone 05.24.09 at 12:02 am

Would this work with uninterruptible processes (ones with a D state code)?

I quite like it, and it could come in handy. Firefox at 5% is almost usable. :-p

4 absolut 05.26.09 at 6:59 pm

may you help me about this question? pls

Create a C program which starts two processes.These processes work with common memory.The first process generates 20 random integer numbers.The second process reads them and writes the odd numbers in file F1, the even numbers in file F2

5 nima chavoshi 06.25.09 at 11:19 am

thanks a lot, it’s so nice :)

6 Milad 07.08.09 at 7:55 pm

hello
How i can use cpulimit for my user on the server . for example my one user can use only 5% of the cpu and other user can use the 10 % of my server cpu?

7 Nazeem S 07.13.09 at 6:59 am

graet

8 Felipe 08.22.09 at 4:49 am

Thanks, but i get an error:

Segmentation fault (core dumped)

An core dump file is created when i run cpulimit for limit apache (httpd).

My server is an Core2Quad 64 Bits…maybe cause i’m using 64 Bits?

Thanks!

9 Slawek 10.07.09 at 10:56 am

I would like to limit CPU usage by command :
gzip -9 somename.gz
How to use cpulimit with it.
# cpulimit -l 30 -e gzip -1 file.gz
cpulimit: invalid option — ‘1′
Any ideas ?
regards to you all

10 Slawek 10.07.09 at 11:09 am

Sorry original command was:
gzip -1 file.gz
# cpulimit -l 30 -e gzip -1 file.gz
cpulimit: invalid option — ‘1′

11 J. Ruiz 10.21.09 at 8:16 am

@Slawek

At current version of cpulimit (1.1) does *not* support programs execution directly from cpulimit.
You have two ways to do it:
- First, limit the cpu usage of gzip command and then, run gzip.
- Run gzip and then limit its cpu usage with cpulimit.

Only development version support programs execution directly.

12 Josh 10.22.09 at 4:14 pm

I am trying to limit the gzip process, however, it isn’t currently running. Is there a way to make it so whenever a process runs it is limited?

13 J. Ruiz 10.22.09 at 10:23 pm

@Josh

Run this “cpulimit -l -e gzip”. Leave it running, for example, in a detached screen or in background adding the character “&” at the end.

14 karunakar 11.23.09 at 4:53 pm

Hi,

I tried to install this and use. but i am getting “Segmentation fault (core dumped)” error.
How do I solve this?

Thanks & regards,
karunakar gowni

15 O2Link 12.08.09 at 5:42 pm

Thank you!

16 Pugazendhi Asaimuthu 12.23.09 at 8:31 pm

tar cpulimit-1.1.tar.gz
returned, on CentOS, “/bin/tar: Old option `g’ requires an argument.”
The following worked.
tar -xvzf cpulimit-1.1.tar.gz

17 Vivek Gite 12.24.09 at 5:32 am

@ Pugazendhi,

Thanks for the heads up, it was a typo on my part!

18 NixNoob 03.09.10 at 5:32 pm

Hi, I am fairly new to Linux. I followed your instructions and was able to install cpulimit – Thanks! My question is: I have a scheduled backup job that starts/stops a “java” process which hammers the CPU. Is there a way to auto-limit the the CPU usage of the java process when it starts?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ: