cpulimit is a small program written in C that allows to limit CPU usage by Linux process. Limit is specified in percentage so it’s possible to prevent high CPU load generated by scripts, programs or processes.

I found cpulimit pretty useful for the scripts running from cron, for example I can do overnight backups and be sure that compression of 50GB file via gzip won’t eat all CPU resources and all other system processes will have enough CPU time.

In most of Linux distributions cpulimit is available from binary repositories so you can install it using commands:

sudo apt-get install cpulimit

or

sudo yum install cpulimit

If it’s not possible in your distro then it’s extremely easy to compile it:

cd /usr/src/
wget --no-check-certificate https://github.com/opsengine/cpulimit/tarball/master -O cpulimit.tar
tar -xvf cpulimit.tar
cd opsengine-cpulimit-9df7758
make
ln -s cpulimit /usr/sbin/cpulimit

From that moment you can run commands limited by CPU percentage, e.g. below command executes gzip compression so that gzip process will never step over 10% of CPU limit:

/usr/sbin/cpulimit --limit=10 /bin/gzip vzdump-openvz-102-2012_06_26-19_01_11.tar

You can check actual CPU usage by gzip using commands:

ps axu | grep [g]zip

or

top

Btw, the first command contains ‘grep [g]zip’ to avoid the last line in common output:

root    896448  10.0  3.1 159524  3528 ?        S    13:12   0:00 /usr/sbin/cpulimit --limit=10 /bin/gzip vzdump-openvz-102-2012_06_26-19_01_11.tar
root       26490  0.0  0.0   6364   708 pts/0    S+   15:24   0:00 grep gzip

Using cpulimit you can also allocate CPU limit to already running processes, e.g. below command will allocate 20% CPU limit to process with PID 2342:

/usr/sbin/cpulimit -p 2342 -l 20

It’s possible to specify process by its executable file instead of PID:

/usr/sbin/cpulimit -P /usr/sbin/nginx -l 30

 

5 Comments

 

  1. July 8, 2012  1:37 pm by smith3v Reply

    Hi, Artem.

    Could you possibly specify in what kind the cpulimit utility is better then nice (and ionice for IO-bound tasks)? Why don't you just use the standard utilities?

    Thanks for the article.

    • July 8, 2012  5:41 pm by Artem Nosulchik Reply

      Hi Vyacheslav,

      Sure. nice/ionice sets priority of the process. In contrast, cpulimit sets percentage of CPU time available to the process. Hint: modify firefox's shortcut to smth like 'cpulimit -P /opt/firefox/firebox -l 20' so it never use more than 20% of CPU time.

  2. March 22, 2013  9:16 pm by book flights ticket Reply

    I do consider all of the ideas you've offered for your post. They are really convincing and can definitely work. Nonetheless, the posts are very brief for newbies. May you please prolong them a bit from next time? Thank you for the post.| book flights ticket http://www.funtraveldeals.com/book-flights/

  3. March 22, 2013  9:46 pm by agate cameos jewellery Reply

    Does your website have a contact page? I'm having a tough time locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it expand over time.| agate cameos jewellery http://www.goldmemoriesforever.com

  4. May 7, 2013  7:39 pm by DebraHughes Reply

    Excuse for that I interfere ? At me a similar situation. Is ready to help.

Leave a reply

 

Your email address will not be published.