blog.up-link.ro
1May/100

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

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 (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.