How to Limit The CPU Usage of Any Process in Linux

Have 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:

To restrict a process, use the command

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 .

cpulimit-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%.

cpulimit-start-process

cpulimit-system-monitor-after

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

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.

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

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

cpulimit-edit-daemon

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.

Copy the cpulimit_daemon.sh file to the /usr/bin/ folder

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

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

You can open a terminal and type:

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

Alternatively, stop it with:

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

1. Stop cpulimit daemon

2. Remove daemon from boot-up procedure

3. Delete boot-up procedure

4. Delete cpulimit daemon

5. Uninstall cpulimit program

Optionally, uninstall gawk program

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

Code credit: abcuser from Ubuntu Forum