Tutorial

How To Limit CPU Usage On Ubuntu 12.10

Published on June 8, 2013
author

Bulat Khamitov

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!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Bulat Khamitov

author

Still looking for an answer?

Ask a questionSearch for more help

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,

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 14, 2014

@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%

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.