// Tutorial //

How To Limit CPU Usage On Ubuntu 12.10

Published on June 8, 2013
Default avatar
By Bulat Khamitov
Developer and author at DigitalOcean.
How To Limit CPU Usage On Ubuntu 12.10

Limiting CPU usage can be important to prevent your server from crashing.

This is especially useful for any custom scripts you might be running in a crontab.

First, we will spin up a Ubuntu 12.10 x64 droplet:

Install cpulimit

apt-get -y install cpulimit

Usage Syntax

Usage: cpulimit TARGET [OPTIONS...]
   TARGET must be exactly one of these:
      -p, --pid=N        pid of the process
      -e, --exe=FILE     name of the executable program file
      -P, --path=PATH    absolute path name of the executable program file
   OPTIONS
      -b  --background   run in background
      -l, --limit=N      percentage of cpu allowed from 0 to 100 (mandatory)
      -v, --verbose      show control statistics
      -z, --lazy         exit if there is no suitable target process, or if it dies
      -h, --help         display this help and exit

Benchmark CPU Usage

Lets benchmark our CPU usage without cpulimit.

Here is an example of how to utilize your CPU with an application:

md5sum /dev/zero &

This 'forks' the md5sum process into background. You can now see CPU usage with top:

As you can see it is consuming almost 100% of CPU resources (given that we have one CPU core on this droplet).

We can bring up this process to foreground using fg and cancel it with CTRL+C:

Limit CPU usage with cpulimit

Now we can test cpulimit to see if it actually does what it is supposed to.

Lets limit our CPU usage to 40% and run same command:

cpulimit -l 40 md5sum /dev/zero &

Sure enough, it is limited to 40%:

Multi-core Droplets

On droplets with multiple processors, you would need to limit CPU usage on each process.

Here is a script that forks 4 processes without any restrictions and lets them run concurrently on your server:

for j in `seq 1 4`; do md5sum /dev/zero & done

Each CPU core is using almost 100% of resources:

top - 23:29:28 up 7 days, 13:54,  1 user,  load average: 0.80, 1.08, 0.53
Tasks:  77 total,   5 running,  72 sleeping,   0 stopped,   0 zombie
%Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  : 93.2 us,  6.8 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 95.0 us,  5.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 98.3 us,  1.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   8178228 total,   380196 used,  7798032 free,    28136 buffers
KiB Swap:        0 total,        0 used,        0 free,   251708 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                                                                                                        
 8400 root      20   0  7172  612  520 R 101.7  0.0   0:03.10 md5sum                                                                                                                                                                         
 8401 root      20   0  7172  612  520 R 101.7  0.0   0:03.10 md5sum                                                                                                                                                                         
 8399 root      20   0  7172  616  520 R  98.4  0.0   0:03.06 md5sum                                                                                                                                                                         
 8402 root      20   0  7172  612  520 R  98.4  0.0   0:03.09 md5sum                                                                                                                     

To use cpulimit for each process, place it in front of the command:

for j in `seq 1 4`; do cpulimit -l 40 md5sum /dev/zero & done

Now each process uses at most 40% of each thread, and does not overload the server:

top - 23:31:03 up 7 days, 13:55,  1 user,  load average: 2.68, 1.72, 0.82
Tasks:  81 total,   5 running,  76 sleeping,   0 stopped,   0 zombie
%Cpu0  : 39.4 us,  0.7 sy,  0.0 ni, 59.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.3 st
%Cpu1  : 38.7 us,  1.7 sy,  0.0 ni, 59.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 39.4 us,  1.3 sy,  0.0 ni, 59.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 39.4 us,  1.7 sy,  0.0 ni, 58.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   8178228 total,   380452 used,  7797776 free,    28144 buffers
KiB Swap:        0 total,        0 used,        0 free,   251708 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                                                                                                        
 8442 root      20   0  7172  616  520 R  40.4  0.0   0:06.10 md5sum                                                                                                                                                                         
 8440 root      20   0  7172  612  520 R  40.0  0.0   0:06.09 md5sum                                                                                                                                                                         
 8435 root      20   0  7172  616  520 R  39.7  0.0   0:06.09 md5sum                                                                                                                                                                         
 8436 root      20   0  7172  612  520 R  39.7  0.0   0:06.10 md5sum                                                                                          

And you are all done!

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
6 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi Kamal Nasser,

Thanks for your quick response and advice. I will try this soon and if any further help needs, I will comeback again.

Regards,

@ctstanly: You can install cpulimit on CentOS by running the following commands:

sudo su -c 'rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'
yum --enablerepo=epel -y install cpulimit

The rest of the article should work on CentOS.

Hi, I couldn’t find any article about CentOS. How do I setup cpulimit in CentOS 6.5 x64? Is it the same Ubuntu procedure in CentOS? Please advise how to do it in CentOS 6.5 x64.

Thanks

thanks for the tutorial but its bad explained

Hi, i’ve a debian 7 vps with virtualmin with 2 cpu; I’ve tried to set up a cron like:

cpulimit -l 40 /usr/bin/wget -q -O /dev/null http://www.------------.ww/folder1/script.php > /dev/null 2>&1 &

and also:

for j in seq 1 2; do /usr/bin/wget -q -O /dev/null http://www.------------.ww/folder1/script.php > /dev/null 2>&1 & done

but always without success, the cron fires only with:

/usr/bin/wget -q -O /dev/null http://www.------------.ww/folder1/script.php > /dev/null 2>&1

without cpulimit, where is the mistake? Thanks a lot!

Doesn’t seem to work with processes that use multicore. Say a process’s CPU usage is 700%, cpulimit cannot restrict it to 500%