Step By Step to Install CPUlimit on Ubuntu 20.04 LTS

CPU limit is a open source linux command tool. It is used to restrict the CPU usage of a process, when any particular process consumes more CPU usage & affect the overall performance. The amount of CPU usage will be distributed & controlled by sending SIGSTOP & SIGCONT POSIX signals.

There are few steps to install CPUlimit on ubuntu:

Step 1: Update the System.

apt-get update

Step 2: Install CPUlimit on system.

apt-get install cpulimit

  • Here is the command output.

root@ip-172-31-26-207:/home/ubuntu# apt install cpulimit
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
cpulimit
0 upgraded, 1 newly installed, 0 to remove and 24 not upgraded.
Need to get 17.4 kB of archives.
After this operation, 51.2 kB of additional disk space will be used.
Get:1 http://sa-east-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 cpulimit amd64 2.6-2 [17.4 kB]
Fetched 17.4 kB in 0s (127 kB/s)
Selecting previously unselected package cpulimit.
(Reading database ... 63754 files and directories currently installed.)
Preparing to unpack .../cpulimit_2.6-2_amd64.deb ...
Unpacking cpulimit (2.6-2) ...
Setting up cpulimit (2.6-2) ...
Processing triggers for man-db (2.9.1-1) ...

Step 3: CPUlimit Syntax & Examples:

cpulimit -p pid
cpulimit -e executablename
cpulimit -P /path-to-executable

Where

-p or --pid: process ID of a process.
-e or --exe: Name of the executable file.
-P or --path: Absolute path of the executable file.

  • To list all the available option in CPUlimit.

cpulimit -h

  • Here is the command output.

root@ip-172-31-26-207:/home/ubuntu# cpulimit -h
CPUlimit version 2.4
Usage: cpulimit TARGET [OPTIONS...] [-- PROGRAM]
TARGET must be exactly one of these:
-p, --pid=N pid of the process
-e, --exe=FILE name of the executable program file
The -e option only works when
cpulimit is run with admin rights.
-P, --path=PATH absolute path name of the
executable program file
OPTIONS
-b --background run in background
-f --foreground launch target process in foreground and wait for it to exit
-c --cpu=N override the detection of CPUs on the machine.
-l, --limit=N percentage of cpu allowed from 1 up.
Usually 1 - 200, but can be higher
on multi-core CPUs (mandatory)
-m, --monitor-forks Watch children/forks of the target process
-q, --quiet run in quiet mode (only print errors).
-k, --kill kill processes going over their limit
instead of just throttling them.
-r, --restore Restore processes after they have
been killed. Works with the -k flag.
-s, --signal=SIG Send this signal to the watched process when cpulimit exits.
Signal should be specificed as a number or
SIGTERM, SIGCONT, SIGSTOP, etc. SIGCONT is the default.
-v, --verbose show control statistics
-z, --lazy exit if there is no suitable target process,
or if it dies
-- This is the final CPUlimit option. All following
options are for another program we will launch.
-h, --help display this help and exit

  • To Run the following command to display the process ID.

dd if=/dev/zero of=/dev/null &

  • Here is the command output.

root@ip-172-31-26-207:/home/ubuntu# dd if=/dev/zero of=/dev/null &
[1] 1838

  • To displays information about CPU and memory utilization.Run top command:

top

  • Here is the command output.

top - 05:39:19 up 5 min, 1 user, load average: 0.57, 0.23, 0.08
Tasks: 118 total, 2 running, 116 sleeping, 0 stopped, 0 zombie
%Cpu(s): 30.6 us, 19.5 sy, 0.0 ni, 49.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3928.9 total, 3181.1 free, 170.3 used, 577.5 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 3531.5 avail Mem
PID  USER  PR  NI VIRT   RES   SHR  S  %CPU  %MEM  TIME+ COMMAND
1838 root  20  0  7280   720   656  R  100.0 0.0   0:28.30 dd
183  root  20  0  11008  3812  3168 R  0.3   0.1   0:00.02 top
1    root  20  0  102848 12272 8196 S  0.0   0.3   0:04.20 systemd
2    root  20  0   0      0     0   S  0.0   0.0   0:00.00 kthreadd

  • To limit the CPU Usage of a Process.

cpulimit -p pid --limit 50
cpulimit --pid 1838 --limit 20
cpulimit -p 1838 --limit 50

  • Here is the command output.

root@ip-172-31-26-207:/home/ubuntu# cpulimit --pid 1838 --limit 50
Process 1838 detected

  • Run cpulimit as a background process.

cpulimit --pid 1838 --limit 20 --background

  • Number of CPU cores present on the system.

cpulimit --pid 1838 --limit 20 --cpu 4

  • To Kill a Process Using Its PID.

cpulimit --pid 1838 --limit 20 --kill 0r -k

  • Here is the command output.

root@ip-172-31-26-207:/home/ubuntu# cpulimit --pid 1838 --limit 20 -k
Process 1838 detected
Process 1838 killed.
[1]+ Killed dd if=/dev/zero of=/dev/null

Leave a Reply