CPULimit is an effective tool for managing and restricting the CPU usage of your VPS server. It allows you to limit individual processes that may be consuming excessive CPU resources, potentially impacting other processes on the server. Additionally, it helps in better managing CPU usage across multiple CPU-intensive processes, while ensuring some CPU capacity remains available for unexpected tasks.
Installing CPULimit
For Ubuntu/Debian:
sudo apt-get install cpulimit
For CentOS:
sudo yum install epel-release
sudo yum install cpulimit
Using CPULimit
As an example, let's run the dd command to create a CPU-intensive process and then use CPULimit to restrict its CPU usage.
Running the dd command:
dd if=/dev/zero of=/dev/null &
Checking the dd process usage and PID:
top
You should see something similar to the following:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
19739 root 20 0 108096 616 520 R 39.5 0.0 0:09.48 dd
In this example, the CPU usage for the dd process is 39.5%. To limit this process's CPU usage to 15%, use the following command:
cpulimit -l 15 -p 19739 &
The -l argument specifies the percentage of CPU to be allocated.
The -p argument specifies the process ID.
After running this command, you can use top again to verify the CPU usage, which should now be around 15% (with a small variation of +/- 2%).
That's it! You now know how to use CPULimit to manage CPU utilization for specific processes on your server.