blog.up-link.ro android bsd linux unix & open-source world

1May/103

How To Limit CPU Usage of A Process with cpulimit under Linux

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed or subscribe via E-mail for updates on this topic.

In this tutorial I'll show you how you can limit the CPU usage of a process with cpulimit tool on Debian/Ubuntu Linux.
cpulimit is a program that limit the CPU Usage of a process (expressed in percentage, not in CPU Time). This is very useful to control batch jobs, when you don't want them to use to much CPU. cpulimit is able to adapt itself to the overall system load, dynamically and quickly.

1. Installing cpulimit

cpulimit tool is available as a package for Debian and Ubuntu.
If you are using Ubuntu you have to become root first:

# sudo su -

You can install cpulimit by running the following command:

# apt-get install cpulimit

2. Using cpulimit

Take a look at the cpulimit manual to learn how to use it:

# man cpulimit

NAME
cpulimit -- limits the CPU usage of a process

SYNOPSIS
cpulimit TARGET [OPTIONS...]

DESCRIPTION
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

-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

EXAMPLES
Assuming  you  have  started "foo --bar" and you find out with top(1) or ps(1) that this process uses all your CPU
time you can either

# cpulimit -e foo -l 50
limits the CPU usage of the process by acting on the executable program file (note: the argument "--bar" is
omitted)

# cpulimit -p 1234 -l 50
limits the CPU usage of the process by acting on its PID, as shown by ps(1)

# cpulimit -P /usr/bin/foo -l 50
same as -e but uses the absolute path name

AUTHOR
This  manpage was written for the Debian project by gregor herrmann <gregor+debian@comodo.priv.at> but may be used
by others.

Examples and explanations:

Let's assume we want to limit apache2 to 25%. You can do this by running the following command:

# cpulimit -e apache2 -l 25

The -e switch takes the name of the executable. You can take that name from the output of the 'ps aux' or 'top' command.

Instead of using the name of the executable, we can use pid with the -p switch. You can find out the process ID of the apache2 process as follows:

# ps aux

or

# ps aux | grep apache2

Let's assume the apache2 process ID is 7410. we can limit that process to 25% CPU usage as follows:

# cpulimit -p 7410 -l 25

Instead of using the name of the executable file (-e) of the pid (-p), we can also use the absolute path name of the executable program with the -P switch. The absolute path name of the apache2 executable is /usr/sbin/apache2 so we'd use the following command:

# cpulimit -P /usr/sbin/apache2 -l 25

You can find the absolute patch of a program by using 'which' or 'type' command:

# which apache2
/usr/sbin/apache2

# type apache2
apache2 is /usr/sbin/apache2

NOTE: cpulimit will run in the foreground of your terminal until you terminate it with CTRL+C - terminating it will also remove any CPU limits.

Print This Post Print This Post
Comments (3) Trackbacks (0)
  1. Pretty nice post.
    I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. I’ll be subscribing to your feed and I hope you write again soon!

  2. It is certainly interesting for me to read the article. I like such topics and anything that is connected to this matter. I definitely want to read more on that blog soon.

    Katherine

  3. Hi,

    I just found your blog on Technorati and Digg upcoming news feeds and read a few of your other posts. It seems the articles are very good.
    Keep up the good work.

    Cheers,
    Michael


Leave a comment


*

No trackbacks yet.