How to limit program CPU usage on Linux
Sometimes when running a Linux server or workstation, you may execute a program that uses way too much CPU power and drains system resources. Thankfully, the CPULimit app exists, and with it, overuse of the CPU on Linux can be stopped. In this guide, we’ll show you how to set up CPUlimit to block specific applications from overusing the CPU.
Install CPULimit
CPULimit is a potent application. It works on both Linux workstations/desktops and servers. However, this program isn’t installed on many of today’s most popular Linux operating systems by default. So, before we go over how to use this program, we’ll need to go over how to install the program.
To start the installation of CPULimit on your Linux operating system, open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. From there, follow the command-line instructions outlined below to get the program running on the OS you currently use.
Ubuntu
CPULimit is readily available in the “Universe” software repository. To get the CPULimit application installed, use the following Apt command in a terminal window.
sudo apt install cpulimit
Debian
For Debian, CPULimit is located in the “Main” software repository. To install the application on your system, use the Apt-get command below.
sudo apt-get isntall cpulimit
Arch Linux
Using CPULimit on Arch Linux requires enabling the “Community” software repository. To enable it, start by opening your Pacman configuration file in the Nano text editor.
sudo nano -w /etc/pacman.conf
Using the Down Arrow scroll down to “Community” and remove the # symbol from in front of it. Do the same for the text lines directly below it. When done, save the edits to the configuration file with the Ctrl + O button, and exit Nano by pressing Ctrl + X.
After editing the Pacman configuration file on Arch, you must re-sync the package manager with the Arch repos, using the command below to finish enabling “Community.”
sudo pacman -Syy
Once “Community” is ready to go, you’ll be able to install the software with:
sudo pacman -S cpulimit
Fedora
CPULimit is in the primary Fedora Linux software repositories. To install the application on your Fedora system, use the Dnf command below.
sudo dnf install cpulimit
OpenSUSE
Need to get the CPULimit application up and running on your OpenSUSE Linux system? If so, you’ll be able to install it from the “Oss all” repository by using the zypper command below.
sudo zypper install cpulimit
Limiting process use with CPULimit
The way CPULimit controls individual process on a Linux system is by process ID. So, to limit a program that is running with too much CPU usage, we must find out the exact process ID. There are many ways to figure out the exact process ID of a given program on Linux. In this guide, we’ll go over two of the most reliable methods.
PS AUX
A foolproof way to find a running process ID on Linux is with the ps command, as it shows a list of the running processes on the system. To find any process, open up a terminal window and run theĀ ps aux command.
ps aux
Look through the massive list of running processes for the program that is using too much CPU power on your Linux system. Once you’ve found the program, look under the “PID” column, and take note of the number, as the PID (process ID) is used by CPULimit to curb problematic programs.
Need more help sorting through the list of running processes on your Linux system? Try combining the ps aux command with grep and the name of the program. It’ll filter out only that app, and return your process ID. For example:
ps aux | grep program-name
Pidof
Another way to find the process ID of a running process or program on a Linux system is with the pidof command. To use this tool, write in pidof followed by the name of the program (or your best guess). It’ll instantly return the exact process ID necessary for CPULimit to work right.
pidof program-name
Once the process ID for the program is known, the hard part is over. All that is left is to put that number into a command that will limit how much CPU power it can use.
For example, to limit process 18976 only to use 15% of my CPU, run the following command in a terminal window.
sudo cpulimit -p 18976 -l 15
To limit any process with CPULimit on a Linux machine, fill out the command example below with your process ID, and percentage.
sudo cpulimit -p process-ID -l percentage-number
Need to stop CPULimit from limiting a process on your Linux system? Press Ctrl + C on the keyboard to instantly halt the program. As soon as CPULimit is terminated, the process will go back to normal usage.