The cpulimit tool curbs the CPU usage of a process by pausing the process at different intervals to keep it under the defined ceiling. It does this by sending SIGSTOP and SIGCONT signals to the process. It does not change the nice value of the process, instead it monitors and controls the real-world CPU usage.
cpulimit is useful when you want to ensure that a process doesn’t use more than a certain portion of the CPU. The disadvantage over nice is that the process can’t use all of the available CPU time when the system is idle.
To install it on CentOS type:
wget -O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zip
sudo cp src/cpulimit /usr/bin
The commands above will download the source code from GitHub, unpack the archive file, build the binary, and copy it to /usr/bin.
cpulimit is used in a similar way to nice, however you need to explicitly define the maximum CPU limit for the process using the ë-lí parameter. For example:
cpulimit -l 50 matho-primes 0 9999999999 > /dev/null &
Note how the matho-primes process is now only using 50% of the available CPU time. On my example system the rest of the time is spent in idle.
You can also limit a currently running process by specifying its PID using the ë-pí parameter. For example
cpulimit -l 50 -p 1234
Where 1234 is the PID of the process.