Cyberithub

How to Limit CPU Usage of a Process with CPULimit in Linux (RHEL/CentOS 7/8)

Advertisements

In this article I will explain how to limit CPU Usage of a Process with cpulimit in Linux. Sometimes in Production Systems you might have seen a scenario where a Process will start spiking CPU Utilization, due to which another important process which also requires significant CPU Cycles unable to run properly and hence start creating issues. In those kind of scenarios you can use a free opensource tool known as CPULimit in Linux to limit the CPU utilization of any Process ID(PID). In this session, I will take you through the installation and different usages of this tool.

Synopsis

cpulimit [OPTIONS...] TARGET

How to Limit CPU Usage of a Process with CPULimit in Linux (RHEL/CentOS 7/8) 1

Limit CPU Usage of a Process with CPULimit Tool in Linux

Also Read: 10 Popular Modprobe command examples in Linux/Unix (View, Install and Remove Modules)

Step 1: Prerequisites

a)You need to have a running Linux (RHEL/CentOS 7/8) System.

b)You should have yum available in your System. You can check Top 22 YUM Command Examples in RedHat/CentOS 7 to know more about yum command.

c)You should have sudo access to run all privileged commands. You can check How to Add User to Sudoers to know more about providing sudo access to the User.

Step 2: Update Your System

You need to first update your system with the latest available updates using yum update -y command as shown below. This command will connect configured repository and will look for any available updates. Once it detects the updates, it will find and install the dependency as well using that simple yum update command.

[root@localhost ~]# yum update -y
Resolving Dependencies
--> Running transaction check
---> Package ca-certificates.noarch 0:2019.2.32-76.el7_7 will be updated
---> Package ca-certificates.noarch 0:2020.2.41-70.0.el7_8 will be an update
---> Package kernel.x86_64 0:3.10.0-1127.13.1.el7 will be installed
---> Package kernel-headers.x86_64 0:3.10.0-1127.10.1.el7 will be updated
---> Package kernel-headers.x86_64 0:3.10.0-1127.13.1.el7 will be an update
---> Package kernel-tools.x86_64 0:3.10.0-1127.10.1.el7 will be updated
---> Package kernel-tools.x86_64 0:3.10.0-1127.13.1.el7 will be an update
---> Package kernel-tools-libs.x86_64 0:3.10.0-1127.10.1.el7 will be updated
---> Package kernel-tools-libs.x86_64 0:3.10.0-1127.13.1.el7 will be an update
---> Package microcode_ctl.x86_64 2:2.1-61.6.el7_8 will be updated
---> Package microcode_ctl.x86_64 2:2.1-61.10.el7_8 will be an update
---> Package python-perf.x86_64 0:3.10.0-1127.10.1.el7 will be updated
---> Package python-perf.x86_64 0:3.10.0-1127.13.1.el7 will be an update
---> Package rsyslog.x86_64 0:8.24.0-52.el7 will be updated
---> Package rsyslog.x86_64 0:8.24.0-52.el7_8.2 will be an update
---> Package unbound-libs.x86_64 0:1.6.6-4.el7_8 will be updated
---> Package unbound-libs.x86_64 0:1.6.6-5.el7_8 will be an update
--> Finished Dependency Resolution
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-957.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
kernel x86_64 3.10.0-1127.13.1.el7 updates 50 M
Updating:
ca-certificates noarch 2020.2.41-70.0.el7_8 updates 382 k
kernel-headers x86_64 3.10.0-1127.13.1.el7 updates 9.0 M
kernel-tools x86_64 3.10.0-1127.13.1.el7 updates 8.0 M
kernel-tools-libs x86_64 3.10.0-1127.13.1.el7 updates 8.0 M
microcode_ctl x86_64 2:2.1-61.10.el7_8 updates 2.7 M
python-perf x86_64 3.10.0-1127.13.1.el7 updates 8.0 M
rsyslog x86_64 8.24.0-52.el7_8.2 updates 621 k
unbound-libs x86_64 1.6.6-5.el7_8 updates 406 k
Removing:
kernel x86_64 3.10.0-957.el7 @anaconda 63 M

Transaction Summary
========================================================================================================================================================================
Install 1 Package
Upgrade 8 Packages
Remove 1 Package

Step 3: Install CPULimit tool in Linux

Now you can Install CPULimit tool in Linux using yum install cpulimit -y command as shown below.

[root@localhost ~]# yum install cpulimit -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 8.2 kB 00:00:00
* base: centos.excellmedia.net
* epel: mirrors.thzhost.com
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package cpulimit.x86_64 1:0.2-1.20151118gitf4d2682.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
cpulimit x86_64 1:0.2-1.20151118gitf4d2682.el7 epel 16 k

Transaction Summary
========================================================================================================================================================================
Install 1 Package

Total download size: 16 k
Installed size: 26 k
Is this ok [y/d/N]: y
Downloading packages:
cpulimit-0.2-1.20151118gitf4d2682.el7.x86_64.rpm | 16 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:cpulimit-0.2-1.20151118gitf4d2682.el7.x86_64 1/1
Verifying : 1:cpulimit-0.2-1.20151118gitf4d2682.el7.x86_64 1/1

Installed:
cpulimit.x86_64 1:0.2-1.20151118gitf4d2682.el7

Complete!

Step 4: Limit CPU Usage of a Process with CPULimit in Linux

Just to show an example we are taking Process ID 1463 running in our System. You can check the process Id 1463 by querying the output of ps -ef command as shown below.

[root@localhost ~]# ps -ef | grep -i 1463 | grep -v grep
root 1463 1462 0 Jun28 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper

If you want to limit the CPU Utilization of PID 1463 then you need to use cpulimit --pid 1463 --limit 2 command as shown below. This command will limit the CPU Utilization of Process ID 1463 to 2%.

[root@localhost ~]# cpulimit --pid 1463 --limit 2
Process 1463 found

--pid : PID of the Process

--limit : Percentage of CPU Allowed from 0 to 100.

Step 5: Limit CPU Usage of a Process with CPULimit in Verbose Mode

If you want to limit the CPU usage of a process with cpulimit command and wanted to visualize the utilization limit then you need to use --verbose option as well as shown below. You can use verbose mode in above example and can see the output as mentioned below.

[root@localhost ~]# cpulimit --pid 1463 --limit 2 --verbose
1 cpu detected
Process 1463 found
Priority changed to -10
Members in the process group owned by 1463: 1

%CPU work quantum sleep quantum active rate
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%

--verbose : show control statistics.

Step 6: Exit if there is no suitable Target Process

If you want the limit to exit out in case there is no suitable target process then you need to use --lazy option with cpulimit command as shown below. In this example, we are taking another PID 1207 which will die after sometime and hence will cause the cpulimit to exit out.

[root@localhost ~]# cpulimit --pid 1207 --limit 2 --verbose --lazy
1 cpu detected
Process 1207 found
Priority changed to -10
Members in the process group owned by 1207: 1

%CPU work quantum sleep quantum active rate
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
0.00% 100000 us 0 us 100.00%
No more processes.

--lazy : exit if there is no target process, or if it dies.

Step 7: Check all the CPULimit Options in Linux

If you want to check all the CPULimit options in Linux then you need to use cpulimit -h command as shown below.

[root@localhost ~]# cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
OPTIONS
-l, --limit=N percentage of cpu allowed from 0 to 100 (required)
-v, --verbose show control statistics
-z, --lazy exit if there is no target process, or if it dies
-i, --include-children limit also the children processes
-h, --help display this help and exit
TARGET must be exactly one of these:
-p, --pid=N pid of the process (implies -z)
-e, --exe=FILE name of the executable program file or path name
COMMAND [ARGS] run this command and limit it (implies -z)

 

 

 

 

 

Popular Recommendations:-

How to Set MariaDB root password in RHEL/CentOS 7/8 Using Simple mysql_secure_installation

How to Install AWS CLI in Linux (RedHat/CentOS 7/8) Using 6 Easy Steps

11 Useful LVM(lvcreate, pvcreate and vgcreate) command examples on Linux

How to Install and Configure FTP Server(vsftpd) in Linux (RedHat/CentOS 7/8) Using 8 Easy Steps

20 Useful Linux History Command Examples | Bash Command History

How to Install PHP on Ubuntu 18.04

How to Install Rust Programming Language in Linux Using 6 Best Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

How to Install Ruby on CentOS/RedHat 7 in 5 Easy Steps

33 Practical Examples of ulimit command in Linux/Unix for Professionals

Install Node.js in 6 Easy Steps on Ubuntu 18.04

How to Install NVM for Node.js on Ubuntu 18.04

Limit CPU Usage of a Process in Linux with CPULimit tool 

Leave a Comment