How to limit CPU usage with CPULimit on Ubuntu Linux
The cpulimit command is designed to restrict the CPU usage of a specific process on Linux, ensuring that it does not exceed a defined threshold. This can be particularly useful for managing system resources and preventing a single process from monopolizing CPU time, which could degrade overall system performance. By specifying the desired CPU usage limit as a percentage, users can apply constraints to running processes or initiate new ones under controlled CPU consumption. The tool sends SIGSTOP and SIGCONT signals to the target process, pausing and resuming them to maintain the set usage limit.
1 Preliminary Note
This tutorial has been tested on Ubuntu 24.04, but it also works on the older Ubuntu releases.
2 CPULimit Installation
Firstly, we need to install cpulimit as follows:
sudo apt update
sudo apt install cpulimit
3 Limiting CPU usage
Now, we will check the utility for limiting CPU usage. For this, we will first check the CPU usage without CPUlimit and then implement CPUlimit to evaluate it. Let's make it clear with an example.
- Here is an example of how to utilize your CPU with an application in single-core CPU:
dd if=/dev/zero of=/dev/null &
Then, we will check the CPU usage with the command:
top
top - 11:24:18 up 49 min, 1 user, load average: 0.94, 1.02, 1.79
Tasks: 249 total, 2 running, 247 sleeping, 0 stopped, 0 zombie
%Cpu(s): 13.4 us, 11.6 sy, 0.0 ni, 74.9 id, 0.0 wa, 0.1 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 271652 used, 738888 free, 21760 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 158204 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1850 root 20 0 7224 616 520 R 100.0 0.1 1:20.33 dd
1851 root 20 0 24952 1756 1180 R 0.3 0.2 0:00.03 top
1 root 20 0 33480 2776 1456 S 0.0 0.3 0:05.31 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
As we can see that, CPU usage has gone to 100%. Now, we will use the cpulimit to limit CPU usage. We can bring up this process to the foreground using fg command and cancel it with CTRL+C
fg

Now we can test cpulimit to see if it does what it should. Let's test it as follows:
cpulimit -l 30 dd if=/dev/zero of=/dev/null &root@server1:~# cpulimit -l 30 dd if=/dev/zero of=/dev/null &
[1] 1852
root@server1:~# Process 1853 detected
[1]+ Done cpulimit -l 30 dd if=/dev/zero of=/dev/null
root@server1:~#
Now we will check the CPU usage with top command:
toptop - 11:30:54 up 55 min, 1 user, load average: 0.20, 0.58, 1.34
Tasks: 250 total, 2 running, 247 sleeping, 1 stopped, 0 zombie
%Cpu(s): 4.5 us, 4.1 sy, 0.0 ni, 91.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 271944 used, 738596 free, 21816 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 158212 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1853 root 20 0 7224 612 520 T 33.8 0.1 0:35.53 dd
1 root 20 0 33480 2776 1456 S 0.0 0.3 0:05.37 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0
Now you can see that the CPU usage has decreased from 100% to 33.8%, almost 30%. So we have successfully checked the utility cpulimit, which can limit the usage of CPU consumption in a single-core Ubuntu distro.
- Here is an example of how to utilize your CPU with an application in multiple core CPU:
To check your CPU core, use the command:
nproc
In my case, it is CPU core count was 4.
Now we will proceed to check the CPU usage without cpulimit in all 4 cores for the application as follows:
for j in `seq 1 4`; do dd if=/dev/zero of=/dev/null & done
It will run the command utilizing all the cores and yeild the output as:
[1] 1263
[2] 1264
[3] 1265
[4] 1266
root@server1:~#
Now check the CPU usage with the top command:
toptop - 11:47:45 up 4 min, 1 user, load average: 3.63, 1.53, 0.57
Tasks: 290 total, 5 running, 285 sleeping, 0 stopped, 0 zombie
%Cpu0 : 48.3 us, 51.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st
%Cpu1 : 47.8 us, 52.2 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 53.3 us, 46.4 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st
%Cpu3 : 52.0 us, 48.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 209712 used, 800828 free, 20276 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 93632 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1263 root 20 0 7224 612 520 R 100.0 0.1 2:21.40 dd
1264 root 20 0 7224 616 520 R 100.0 0.1 2:21.41 dd
1265 root 20 0 7224 612 520 R 99.0 0.1 2:21.03 dd
1266 root 20 0 7224 616 520 R 98.0 0.1 2:20.82 dd
1281 root 20 0 104416 3992 2920 S 1.0 0.4 0:00.03 sshd
1283 root 20 0 104416 3988 2920 S 1.0 0.4 0:00.03 sshd
1279 root 20 0 104556 4008 2924 S 0.7 0.4 0:00.08 sshd
The dd command consumes almost 100% CPU of all the cores. Next, we will check the command with the cpulimit utility. For this, kill previous traces for the dd command as follows:
killall ddroot@server1:~# killall dd
[1] Terminated dd if=/dev/zero of=/dev/null
[3]- Terminated dd if=/dev/zero of=/dev/null
[2]- Terminated dd if=/dev/zero of=/dev/null
[4]+ Terminated dd if=/dev/zero of=/dev/null
root@server1:~#
Now use cpulimit with the same command as follows:
for j in `seq 1 4`; do cpulimit -l 20 dd if=/dev/zero of=/dev/null & doneroot@server1:~# for j in `seq 1 4`; do cpulimit -l 20 dd if=/dev/zero of=/dev/null & done
[1] 1429
[2] 1430
[3] 1431
[4] 1432
root@server1:~# Process 1434 detected
Process 1433 detected
Process 1437 detected
Process 1439 detected
[1] Done cpulimit -l 20 dd if=/dev/zero of=/dev/null
[2] Done cpulimit -l 20 dd if=/dev/zero of=/dev/null
[3]- Done cpulimit -l 20 dd if=/dev/zero of=/dev/null
[4]+ Done cpulimit -l 20 dd if=/dev/zero of=/dev/null
root@server1:~#
Now check the CPU usage wit the cpulimit utility.
toptop - 11:59:10 up 16 min, 2 users, load average: 0.47, 0.71, 0.81
Tasks: 256 total, 2 running, 251 sleeping, 3 stopped, 0 zombie
%Cpu0 : 2.0 us, 2.0 sy, 0.0 ni, 96.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 26.2 us, 22.8 sy, 0.0 ni, 50.7 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st
%Cpu2 : 14.0 us, 12.3 sy, 0.0 ni, 73.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 13.3 us, 11.6 sy, 0.0 ni, 75.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 1010540 total, 204064 used, 806476 free, 20408 buffers
KiB Swap: 1048572 total, 0 used, 1048572 free. 98340 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1433 root 20 0 7224 612 520 T 28.2 0.1 0:12.00 dd
1439 root 20 0 7224 616 520 R 26.6 0.1 0:12.13 dd
1434 root 20 0 7224 612 520 T 25.3 0.1 0:11.97 dd
1437 root 20 0 7224 612 516 T 22.9 0.1 0:11.93 dd
7 root 20 0 0 0 0 S 0.3 0.0 0:00.22 rcu_sched
8 root 20 0 0 0 0 S 0.3 0.0 0:00.21 rcuos/0
As you can see above, CPU usage is limited from 100% to 20% almost for multiple-core CPUs.
Congratulations! We have successfully tested the cpulimit for limiting the CPU usage in Ubuntu.
4 Links
- Ubuntu : http://www.ubuntu.com/
- cpulimit : https://github.com/opsengine/cpulimit