1. Abstract
When using a Linux VPS, many hosting providers impose strict limits on CPU usage. If your VPS exceeds these predefined thresholds, you risk having your service suspended. Without proper control, resource-intensive processes can trigger these high-load suspensions. To prevent this, we can use CPULimit, a tool designed to cap the CPU usage of specific processes. This ensures your VPS remains within provider limits and runs stably without the fear of being taken offline for "resource abuse." Today, I’ll share how to use CPULimit to manage your system resources effectively.
2. Introduction to CPULimit
CPULimit is a lightweight and practical utility designed to restrict the CPU usage percentage of a specific process. Unlike adjusting process priorities (such as using the nice command), CPULimit controls the actual CPU consumption by dynamically throttling the execution of the process.
Specifically, CPULimit functions by sending SIGSTOP and SIGCONT signals to the process at calculated intervals. This pauses and resumes the process to ensure it stays within your defined limit. It is particularly useful for long-running batch jobs that need to run in the background without hogging the entire system's computing power.
2.1 Installing CPULimit
First, let's look at how to install CPULimit on various Linux distributions:
On Debian or Ubuntu:
Bash
sudo apt-get install cpulimit
On CentOS, RHEL, or Fedora: First, enable the EPEL repository, then install:
Bash
sudo yum install epel-release
sudo yum install cpulimit
Once installed, you can verify it by running cpulimit -h to view the help documentation.
3. Using CPULimit
CPULimit is very straightforward to use. Below are the common commands and usage patterns:
3.1 View Help Information
Bash
cpulimit -h
This will display all available options, such as -p (PID), -e (executable name), and -l (limit percentage).
3.2 Restricting a Process by PID
Suppose you are running a CPU-intensive command like md5sum. First, start the task:
Bash
md5sum /dev/urandom
If you check top, you will see md5sum consuming nearly 100% of the CPU. To limit this process (assuming the PID is 11699) to 50%:
Bash
cpulimit --pid 11699 --limit 50
3.3 Restricting by Process Name or Path
Instead of finding the PID, you can target the process by its name:
Bash
cpulimit --exe md5sum --limit 50
Or use the absolute path to avoid confusion if multiple programs have similar names:
Bash
cpulimit --path /usr/bin/md5sum --limit 50
3.4 Limiting a Process at Startup
You can launch a program and apply the limit simultaneously:
Bash
cpulimit --limit 50 -- md5sum /dev/urandom
3.5 Terminating Processes that Exceed Limits
If you want to kill a process rather than just throttling it when it hits the limit, use the --kill flag:
Bash
cpulimit --pid 11699 --limit 50 --kill
3.6 Running CPULimit in the Background
To keep monitoring a process without keeping your terminal window open, use the --background (or -b) flag:
Bash
cpulimit --pid 11699 --limit 50 --background
4. Advanced Usage
In real-world scenarios, you can automate CPU management using Bash scripts. For example, you can start a process and immediately limit its CPU usage using the $! variable (which holds the PID of the last background process):
Bash
# Start the process in the background
md5sum /dev/urandom &
# Automatically limit the last started process to 50%
cpulimit --pid $! --limit 50
5. Summary
CPULimit is a powerful and essential tool for any Linux VPS user, especially those hosted on providers with strict CPU quotas. By effectively managing the CPU footprint of your tasks—whether via PID, name, or path—you can prevent server suspensions and maintain a responsive system. With additional features like background execution and automatic exit (--lazy), it offers a flexible solution for system resource management.