How to Use the CPULimit Tool to Restrict Resource Usage of Applications on CentOS 7 Cloud Servers?

21-02-2024 02:59:23

CPULimit is a tool designed for the Linux operating system that restricts the resource usage of applications. This tool is particularly useful in scenarios where it is necessary to prevent an application from consuming excessive resources, thereby slowing down other applications. However, CPULimit may not be suitable for all types of applications, as it operates by periodically starting or stopping the process associated with an application. For instance, CPULimit is not recommended for task-based control applications because it sends signals to stop processes, leading to frequent starts and stops of the application. Nonetheless, CPULimit is applicable for web applications, such as those developed in PHP, Java, and similar languages. The following outlines the installation and usage of CPULimit on a cloud server running the CentOS 7 operating system.

First, download and extract the CPULimit archive.

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

After extraction, install it using the compilation method and wait for the installation to complete.

cd cpulimit-2.5
make
make install

Before using CPULimit, it's necessary to identify the Process ID (PID) of the application you wish to limit. This can be done using the top command.

$ top -c

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]

For example, to limit the application plex to not exceed 15% of CPU usage, the following two commands would be executed, assuming the PID for plex is 1336.

screen -S limitcpu
cpulimit -p 1336 -l 15

screen -S creates a new terminal session for CPULimit, ensuring that it continues to run even after the terminal is closed. To switch to this terminal session upon next login, execute the following command.

screen -r limitcpu

Uninstalling CPULimit is also straightforward, with the command provided below.

cd ~/cpulimit-2.5
make deinstall