How To Limit CPU Usage Of A Process With cpulimit (Debian/Ubuntu)
CPU-Limit-in-Linux

How To Limit CPU Usage Of A Process With cpulimit (Debian/Ubuntu)

This tutorial shows how you can limit the CPU usage of a process with the tool cpulimit on Debian/Ubuntu. cpulimit is a simple program that attempts to limit the CPU usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don’t want them to eat too much CPU. It does not act on the nice value or other scheduling priority stuff, but on the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

1 Preliminary Note

I will run all commands in this tutorial as the root user, so either log in as root directly (Debian) or become root like this (Ubuntu):

sudo su

If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.

cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

2 Installing cpulimit

cpulimit is available as a package for Debian and Ubuntu, so it can be installed as follows:

aptitude install cpulimit

3 Using cpulimit

Take a look at the cpulimit man page 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 but may be used
by others.

Now let’s assume we want to limit the process apache2 to 30%. This is how we do it:

cpulimit -e apache2 -l 30

The -e switch takes the name of the executable program file. You can take that name from the output of the top command.

Instead of using the name of the executable program file, we can use the process ID 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 4510; we can then limit that process to 30% CPU usage as follows:

cpulimit -p 4510 -l 30

Instead of using the name of the executable file (-e) of the process ID (-p), we can also pass the absolute path name of the executable program file to cpulimit 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 30

Please note that cpulimit will run in the foreground of your terminal until you terminate it with CTRL+C – terminating it will also remove any CPU limits.

Related Article

Lite 1 GB VPS

How to Optimize Apache, PHP and MySQL Performance for 1GB RAM VPS

There are lots of requests from the visitors especially from David T. upon how to optimize Apache, php and MySQL/Mariadb […]

MySQL

MySQL Error: : Access denied for user

MySQL Error: : ‘Access denied for user ‘root’@’localhost’ Open & Edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distro. Add skip-grant-tables […]

CPU-Limit-in-Linux

How To Limit CPU Usage Of A Process With cpulimit (Debian/Ubuntu)

This tutorial shows how you can limit the CPU usage of a process with the tool cpulimit on Debian/Ubuntu. cpulimit […]