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:
sudo apt-get install cpulimit
To restrict a process, use the command
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.
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: cpulimit_daemon.sh and cpulimit.
Open the cpulimit_daemon.sh 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, “
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, “
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
sudo cp ~/cpulimit/cpulimit_daemon.sh /usr/bin sudo chmod 700 /usr/bin/cpulimit_daemon.sh
Copy the cpulimit file to /etc/init.d/folder, set the necessary permission and make it run during statup.
sudo cp ~/cpulimit/cpulimit /etc/init.d/ sudo chown root:root /etc/init.d/cpulimit sudo chmod +x /etc/init.d/cpulimit sudo update-rc.d cpulimit defaults
Now, reboot your system. The cpulimit daemon should start automatically.
You can open a terminal and type:
sudo service cpulimit status
to check if the cpulimit daemon is running. If it is not running, start it with the command
sudo service cpulimit start
Alternatively, stop it with:
sudo service cpulimit stop
To uninstall, here’s what you need to do:
1. Stop cpulimit daemon
sudo service cpulimit stop # Stop cpulimit daemon and all cpulimited processes
2. Remove daemon from boot-up procedure
sudo update-rc.d -f cpulimit remove # Remove symbolic links
3. Delete boot-up procedure
sudo rm /etc/init.d/cpulimit # Delete cpulimit boot-up script
4. Delete cpulimit daemon
sudo rm /usr/bin/cpulimit_daemon.sh # Delete cpulimit daemon script
5. Uninstall cpulimit program
sudo apt-get remove cpulimit
Optionally, uninstall gawk program
sudo apt-get remove gawk
For more info, refer to the Ubuntu Forum for more detail.
Code credit: abcuser from Ubuntu Forum