How to Limit The CPU Usage of Any Process in Linux

cpulimit-mainHave you ever experienced the situation where you open one particular application (such as Firefox) and it brings the whole system to a standstill? If you are running a web server, the last thing that you want to see is to have an application crashes the whole system and bring all the websites down.

Cpulimit is an application for Linux that can limit the CPU usage of a process. It is useful if you want to restrict a particular application from taking up too much CPU resources and thereby crashing the system. This can also be useful when you need to run several intensive programs simultaneously.

Note: cpulimit should work for all Linux distro. In this tutorial, we will be using Ubuntu for illustration.


In Ubuntu, you can install

cpulimit via the Ubuntu Software Center, click here to install, or type the following command in terminal:

1 sudo apt-get install cpulimit


To restrict a process, use the command

1 sudo cpulimit -p PID -l CPU%

The PID is the process ID of the running application and CPU% is the percentage (0-100, number only) of CPU resources allowed for the app. You can obtain the PID from System -> Administration -> System Monitor .


From the screenshot above, you can see that the Swiftfox application (a variant of Firefox) takes up 68% of the CPU resources before the CPU limit is set. Let’s see what happen when we limit the CPU usage to 20%.



The % CPU instantly drop below 20% and never did it cross the 20% mark again.

Extending cpulimit – Automating the whole process

Cpulimit is useful when you encounter an application that take up lot of CPU resources, or need to carry out batch job. In addition, you can also set it up to monitor the system for any misbehaving application. This is especially useful in a server setup.

abcuser from Ubuntu Forum has come up with a great script that automates the monitoring of your system and restricts any process that exceed a preset CPU limit. The script also allows you to set blacklist/whitelist for specific applications.

Before we start, make sure you have cpulimit and gawk installed.

1 sudo apt-get install cpulimit gawk

Download the scripts here. Extract the tar file to your Home folder. You should have two files inside the cpulimit folder: and cpulimit.

Open the file in your text editor (gEdit) and change the following:


CPU_LIMIT: This is the maximum CPU resources available to each application. The default value is 20%.

DAEMON_INTERVAL: This is the interval for the script to check the system. The default is set to 3 seconds.

BLACK_PROCESS_LIST: This contain the list of items that specifically want to monitor. You can use the “|” delimiter to include multiple processes. For example, “mysql|firefox|gedit“.

WHITE_PROCESSES_LIST: This contain the list of items that you DON’T WANT to monitor. You can use the “|” delimiter to include multiple processes. For example, “mysql|firefox|gedit“.

Note: One or both of the variables BLACK_PROCESSES_LIST and WHITE_PROCESSES_LIST has to be empty. You can’t have a blacklist and a whitelist at the same time.

Setting up

Copy the file to the /usr/bin/ folder

1 sudo cp ~/cpulimit/ /usr/bin
2 sudo chmod 700 /usr/bin/

Copy the cpulimit file to /etc/init.d/folder, set the necessary permission and make it run during statup.

1 sudo cp ~/cpulimit/cpulimit /etc/init.d/
2 sudo chown root:root /etc/init.d/cpulimit
3 sudo chmod +x /etc/init.d/cpulimit
4 sudo update-rc.d cpulimit defaults

Now, reboot your system. The cpulimit daemon should start automatically.

You can open a terminal and type:

1 sudo service cpulimit status

to check if the cpulimit daemon is running. If it is not running, start it with the command

1 sudo service cpulimit start

Alternatively, stop it with:

1 sudo service cpulimit stop


To uninstall, here’s what you need to do:

1. Stop cpulimit daemon

1 sudo service cpulimit stop # Stop cpulimit daemon and all cpulimited processes

2. Remove daemon from boot-up procedure

1 sudo update-rc.d -f cpulimit remove # Remove symbolic links

3. Delete boot-up procedure

1 sudo rm /etc/init.d/cpulimit # Delete cpulimit boot-up script

4. Delete cpulimit daemon

1 sudo rm /usr/bin/ # Delete cpulimit daemon script

5. Uninstall cpulimit program

1 sudo apt-get remove cpulimit

Optionally, uninstall gawk program

1 sudo apt-get remove gawk

For more info, refer to the Ubuntu Forum for more detail.

Code credit: abcuser from Ubuntu Forum

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: