Sometimes you get that one process that just eats up all your resources. In my case, it’s Google-Chrome. I had to find a way to deal with it without resorting to yet another plugin. In this article, I will discuss control a process in both Ubuntu 16 and Centos 7.

Using CPULIMIT to limit CPU usage

CPULIMIT is a command line tool written in C. It is not included in the operating system and has to be installed.

Installation on Ubuntu 16 is quite simple. Execute the following command:

# apt install cpulimit  (Ubuntu 16 has done away with the apt-get and apt-cache, etc syntax. It still works, but why type more than needed?)

On Centos 7 it is just as  easy:

# yum install epel-release ( cpulimit is part of the epel repository )

# yum install cpulimit

Limiting actual usage is done by using the –limit or -l switch option to set the CPU percentage for a process. Please note that total CPU percentage available is usually (100 x Number of CPU Cores). Use top or even better, install htop and use this to find the offending program. CPULIMIT can be invoked in one of three ways, but we will focus on using the NAME of the process first. After you have identified the program in top or htop, you will be able to find the NAME as follows (red arrow on the right):


Now you can use the following command:

# cpulimit -e chrome -l 10

Process 3511 detected (the PID of the initial chrome command)

You can also use the PID (Process ID) as follows:

# cpulimit -p 3511 -l 10

Process 3511 detected

This will result in the process you have restricted running at approximately 10% CPU usage at the most ( in reality it might even reach 11% or 12% ). Granted, this was not a great example, you would normally only want to limit a process that was running away at over 90% CPU usage. Usually, 40% – 50% is a good limit to set. You can set the limiter to run in the background with the –background or  -b  option. If you want it to work every time you start a process, write a small script to start your process and include the cpulimit command as follows:

# Start my process XXX with a limit of 40%
cpulimit -l 10 -- XXX

You could also modify your desktop shortcut to call chrome or whichever program you need to limit

e.g instead of /usr/bin/google-chrome %u you would use /usr/bin/cpulimit -l 10 — /usr/bin/google-chrome %u

Here is the man entry for cpulimit:

CPULIMIT(1) User commands CPULIMIT(1)

 cpulimit -- limits the CPU usage of a process

 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
-P, --path=PATH
      absolute path name of the executable program file

-b, --background
      run cpulimit in the background, freeing up the terminal
-c, --cpu
      specify the number of CPU cores available. Usually this is detected for us.
-l, --limit=N
      percentage of CPU allowed from 1 up. Usually 1 - 100, but can be higher on multi-core CPUs. (mandatory)
-q, --quiet
      Runs in quiet mode, avoids writing update messages to console.
-k, --kill
      kill target process instead of throttling its CPU usage
-r, --restore
      restore a process killed using the -k flag.
-s, --signal
      send an alternative signal to the watched process when we exit. Default is SIGCONT.
-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

Assuming you have started `foo --bar` and you find out with top(1) or ps(1) that this process uses all
your CPU time you can either
# cpulimit -e foo -l 50
      limits the CPU usage of the process by acting on the executable program file (note: the argument
      "--bar" is omitted)
# cpulimit -p 1234 -l 50
      limits the CPU usage of the process by acting on its PID, as shown by ps(1)
# cpulimit -P /usr/bin/foo -l 50
      same as -e but uses the absolute path name
# /usr/bin/someapp
# cpulimit -p $! -l 25 -b
      Useful for scripts where you want to throttle the last command run.
# cpulimit -l 20 firefox
      Launch Firefox web browser and limit its CPU usage to 20%
# cpulimit -c 2 -p 12345 -l 25
      The -c flag sets the number of CPU cores the program thinks are available. Usually this is detected
      for us, but can be over-ridden.
# cpulimit -l 20 -k firefox
      Launch the Firefox program and kill it if the process goes over 20% CPU usage.
# cpulimit -l 20 -p 1234 -s SIGTERM
      Throttle process 1234 at 20% CPU usage. If cpulimit is forced to exit, it sends the watched process
      the SIGTERM signal.

Using CPUTOOL to limit CPU usage

CPUTOOL is another tool to use to control CPU usage. It also needs to be installed as it does not come as a default. The main difference here is that you can specify the maximum system load allowed when a program is called. An example is shown below:

 cputool -l 7.5 -- rsync -av /home /backup/`date +%Y-%m-%d`/
 Run rsync for a local backup only when the system load does not exceed 7.5.

CPUTOOL would be used more commonly on servers where the overall load average is more important than actual CPU usage. CPUTOOL can also control a whole PID tree (Parent + Child processes) which is also more commonly found on servers than on desktops.

In general, the syntax is very similar to CPULIMIT. The syntax for most use cases would be (note the & at the end – this is to regain your terminal session and background the process):

cputool --cpu-limit %cpu -p process_pid &

Once again, you have quite a few options on how to call this tool and here is the man extract for this tool:

CPUTOOL(8) System Manager's Manual CPUTOOL(8)
     cputool — CPUTool is a utility which manages CPU usage and system load
     cputool [--cpu-limit PCNT] [--load-limit LOAD] [ [--pid PID | --pid-pgrp PID ] | [--] COMMAND ... ]
     Limit the CPU usage of a process or a process group to a given limit and/or suspend processes if the system 
     load exceeds a threshold. CPUTool works by sending SIGSTOP and SIGCONT signals to processes depending
     on the system load.
     These programs follow the usual GNU command line syntax, with long options starting with two dashes (`-').
     A summary of options is included below.
-c, --cpu-limit PCNT
     Specify the maxium CPU the process / process group can use. Expressed as percentage of total
     CPU. Eg. 200 is two full CPUs in a multi processor system. Specify an integer value.
-l, --load-limit LOAD
     Specify the maxium load the system may experience for the process process group to continue running. 
     Specifyng a fractional value is possible (e.g. 3.5).
-p, --pid PID
     Manage the CPU usage of a specific PID. This is the most efficient use of CPUTool as it does not
     have to walk the process tree to look for forks of children.
-P, --pid-pgrp PID
     Manage the CPU usage of a specific PID's entire process group. The same can be achieved by specifying a 
     COMMAND which CPUTool will then execute and manage the process group created by that command.
-v, --verbose
     Increase the amount of messages printed to stderr.
-vv  will additionally show statistical information.
-vvv will addditionally show signals being sent to processes.
-V, --version
     Output version information and exit.
-h, --help
     Display a help page and exit.

Using these tools in the correct way can save your server from freezing due to a runaway process, but be careful not to over-limit a process as this will degrade the performance for your end-users. Ensure that resources are always balanced against actual usage demands and do not use limiting to try to fix an over-usage issue which should be fixed by increasing resources.

Host AfricaHappy Hosting!

we're happy to help!

Talk to a hosting specialist today and discover which options will work best for you.

Call us on +27 21 554 3096
Copyright © 2020 HOSTAFRICA - All rights reserved.

By visiting this website, you agree to its terms of use, which can be accessed by clicking on the following link: Website Terms of use
We Accept: EFT, Debit Cards, Credit Cards and Mobile Payments
Accepted payment methods