Home » Linux Howto » 2 Ways to Limit CPU Usage of a Process in Linux

2 Ways to Limit CPU Usage of a Process in Linux

While administering your Linux system you need to limit the CPU usage of a process because it can consume more CPU usage and affects the performance of the whole system. This tutorial I will show how you can control CPU time of a process on Centos 7 and Ubuntu 16.

Refer AlsoHow to Limit Process at User Level on Linux

1) cpulimit

cpulimit is a nice command-line tool written in C programming language for limiting the CPU usage of a process. It is not present by default so we need to install it.

To install cpulimit on Ubuntu 16.04, do as below

# apt-get install cpulimit

Install on centos 7

# yum install epel-release
# yum install cpulimit

We limit the CPU usage by using the --limit or -l option to set a usage percentage for a process. Before limiting the CPU usage, you need to find the process or the command that is using all your CPU time. There are three ways to use cpulimit, by acting

  • on the executable program with the --executable or -eoption
cpulimit -e program -l %cpu
  • on the absolute path name of the file with the --path or -Poption
cpulimit -P path -l %cpu
  • on the PID of the process with the--pid or -poption
cpulimit -p process_pid -l %cpu

So, we will first check the process/program which consumes the CPU time

# top

Then you can use the following command with the name of the program.

# cpulimit -e vmware-vmx -l 40
Process 3185 detected

You can see the tool has detected automatically the process id of the program. You can limit the cpu usage with the process id as below

# cpulimit -p 3185 -l 40
Process 3185 detected

And you can see the result below

You can run cpulimit in background by using the--background or -boption

2) cputool

You can use cputool which also uses cpulimit to reduce CPU usage.This can be also done by using the load limit. cputool is not present by default so you need to install it on Ubuntu 16

# apt install cputool

To use cputool, you need to respect the syntax below

cputool --cpu-limit %cpu -p process_pid

With the same example, you can use the cputool as below

# cputool --cpu-limit 30 -p 3185

The command will take the control of your terminal so you can use it in background as below:

# cputool --cpu-limit 30 -p 3185 &
[1] 16808

You can see the result as below

With these tools, you can easily control the CPU usage of a process when it is already running. You don't need to kill the processes to reduce the usage but you can simply assign a percentage to not exceed.

1 thought on “2 Ways to Limit CPU Usage of a Process in Linux”... add one

  1. When I am using cpulimit in this format:
    cpulimit -e program -l %cpu,
    it will detect the 1st program that name matches, and have it restricted. What happen I have a bunch of process running of the same program ???

    In my environment, this is the command I have executed:
    cpulimit -e qemu-system-x86_64 -l 3%,

    And this the output of top call:
    top - 22:42:34 up 23:22, 3 users, load average: 26.43, 26.69, 25.88
    Tasks: 214 total, 5 running, 209 sleeping, 0 stopped, 0 zombie
    %Cpu(s): 84.0 us, 16.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
    KiB Mem: 30879432 total, 8491104 used, 22388328 free, 103928 buffers
    KiB Swap: 2095100 total, 0 used, 2095100 free. 596156 cached Mem
    5379 gns3 20 0 984580 672640 14340 S 12.3 2.2 128:02.35 qemu-system-x86
    8431 gns3 20 0 984660 578792 14356 R 12.3 1.9 6:06.29 qemu-system-x86
    8440 gns3 20 0 984660 580036 14468 S 12.3 1.9 6:08.20 qemu-system-x86
    4740 gns3 20 0 984660 670428 14296 T 3.3 2.2 127:44.46 qemu-system-x86
    As you can see, only the process of id 4740 has been restricted.




Leave a Comment