How to Install and Use CPULimit on CentOS 7

Updated on September 28, 2018
How to Install and Use CPULimit on CentOS 7 header image

CPULimit is a Linux utility offered to limit an application's resource usage. It is useful when you want to prevent a single application from slowing down other applications; or stop an application from using an entire core or cores for an extended period of time.

Having said that, CPULimit may not work with all applications as it (essentially) starts/stops processes at intervals to bring the CPU usage (expressed as an average) down. This will be explained in the next section.

How does it work?

CPULimit is not designed to work with applications that, for example, use job control; as they may be killed when CPULimit sends a stop (SIGSTOP) signal. In essence, applications will be turned on/off rapidly in order to limit a program to a desired number of cycles.

Don't worry though - most applications will work. Some applications that can be limited include PHP, Java, and Nginx.

Prerequisites

In order to install CPULimit successfully, you will need:

  • A server running CentOS 7 (64-bit systems only).
  • make, screen and wget.
  • Root access or sudo privileges.

Installing CPULimit

Assuming you have all the prerequisites installed, you should be able to breeze through the installation process. To begin, you will need to download CPULimit and extract the tarball:

cd ~
wget https://astuteinternet.dl.sourceforge.net/project/limitcpu/limitcpu/cpulimit-2.5.tar.gz
tar -xvf cpulimit-2.5.tar.gz

Once the tarball finishes extracting, we can enter the newly created directory and begin compiling CPULimit:

cd cpulimit-2.5

Now, run make to begin compiling CPULimit into a binary:

make

Once this process completes, you will have a binary in the cpulimit-2.5 directory. In order to make it available system-wide, we will need to perform the command below:

make install

It may take some time to complete.

Configuring CPULimit with an application

In order to use CPULimit, we'll need to know the process ID. This is known as the PID. By using top we can see a list of our processes:

top -c

You will see a list of processes and it should look like the following:

Tasks: 130 total,   1 running, 129 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3881740 total,   191952 free,   413472 used,  3276316 buff/cache
KiB Swap:  4063228 total,  4062912 free,      316 used.  2881364 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1336 plex      35  15 1368172  67464   6668 S   0.3  1.7 155:41.58 Plex Plug-in [com.plexapp.system] /usr/lib/plexmediaserver/Resources/Plug-ins-995f1dead+
31345 root      20   0  326572  21844  12784 S   0.3  0.6  86:45.32 docker-containerd --config  /var/run/docker/containerd/containerd.toml
    1 root      20   0  193704   6744   4088 S   0.0  0.2   6:49.22 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    2 root      20   0       0      0      0 S   0.0  0.0   0:01.45 [kthreadd]
    3 root      20   0       0      0      0 S   0.0  0.0   0:12.77 [ksoftirqd/0]
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 [kworker/0:0H]
    7 root      rt   0       0      0      0 S   0.0  0.0   0:13.95 [migration/0]

If we wanted to limit plex, for example, we would need to take note of the PID. In this case, it is 1336. If we want to limit plex to 15% of the CPU, we need to create a screen and execute cpulimit:

screen -S limitcpu
cpulimit -p 1336 -l 15

To exit the screen, use the following combination: Ctrl + A + D. To enter the screen once you exit, simply execute the following:

screen -r limitcpu

The general format to run cpulimit will be below:

cpulimit -p (PROCESS PID) -l (CPU %)

Note: PROCESS PID is the process ID and CPU % is the CPU limit.

Uninstalling CPULimit

Removing CPULimit is simple. Perform the following commands:

cd ~/cpulimit-2.5
make deinstall