How to Limit CPU Usage of a Process in Linux with CPULimit

Limit CPU usage of a process in Linux with CPULimit

CPULimit is a command-line tool designed to restrict the CPU usage of a specific process on Linux systems. It’s particularly useful when you have resource-intensive processes that are hogging CPU time and affecting system performance. CPULimit is effective for controlling CPU usage, it’s also important to consider other system resources and use a holistic approach to resource management.

This article will explore how to use CPULimit to prevent processes from consuming excessive CPU time, which can be particularly useful in shared environments or when running background tasks that shouldn’t interfere with the system’s responsiveness.

Here’s a comprehensive guide to limit CPU usage of a process in Linux.

How to Limit CPU Usage of a Process in Linux with CPULimit?

Installation of CPULimit Package

Limit a Process to 50% CPU Usage

Limiting a Process and Running in Background

Specifying the Number of CPUs

Killing Process if Limit Exceeded

Custom Signal on Limit Exceeds

Limiting Multiple Processes

Limiting a Specific Command

Conclusion

How to Limit CPU Usage of a Process in Linux with CPULimit?

Managing system resources efficiently is crucial for maintaining the stability and performance of Linux servers, especially when running resource-intensive applications. One such tool that can aid in this task is CPULimit, a command-line program designed to restrict the CPU usage of a process.

Installation of CPULimit Package

Before you start, ensure CPULimit is installed on your system. Users can install it utilizing the package manager:

Debian/Ubuntu:

CPULimit is freely available in the default repositories of several Linux systems. For Debian/Ubuntu-based systems, you can install it using the following command:

sudo apt install cpulimit

For Red Hat-based systems, you’ll need to enable the EPEL repository before installing:

yum install epel-releaseyum install cpulimit

Identifying the Process (Optional)

Before you can limit a process’s CPU usage, you need to identify its Process ID (PID). You can use the top command to display a real-time view of system processes and their resource consumption.

top

Look for the process that is using a high percentage of CPU time and note its PID.

CPULimit offers several options to fine-tune its behavior:

Options

Description

-b, –background

Run CPULimit in the background.

-c, –cpu

Specify the number of available CPUs (useful for multi-core systems).

-k, –kill

Kill the process if it exceeds the CPU limit.

-z, –lazy

Exit if the target process dies.

-v, –verbose

Increase verbosity.

Example 1: Limit a Process to 50% CPU Usage

Here’s a basic example of how to limit a process with PID 9923 to 50% CPU usage. Here, -p specifies the process ID (PID) to limit and -l sets the CPU usage limit as a percentage:

sudo cpulimit -p 9923 -l 50

Replace 9923 with the actual PID of the process.

Example 2: Limiting a Process and Running in Background

If you want to run CPULimit in the background so that it doesn’t occupy your terminal, you can use the –background or -b switch. This command limits process 9923 to 50% CPU usage and runs CPULimit in the background. This command will restrict the process to using no more than 50% of a single CPU core:

sudo cpulimit -p 9923 -l 50 -b

This frees up your terminal while CPULimit continues to enforce the CPU usage limit.

Example 3: Specifying the Number of CPUs

Limit a process to 40% CPU usage on a system with 4 cores:

sudo cpulimit -p 1234 -l 40 -c 4

Example 4: Killing Process if Limit Exceeded

Limit a process to 50% CPU usage and kill it if it exceeds the limit:

sudo cpulimit -p 1234 -l 50 -k

Example 5: Custom Signal on Limit Exceed

Limit a process to 60% CPU usage and send a custom signal (SIGTERM) if it exceeds the limit:

sudo cpulimit -p 1234 -l 60 -s SIGTERM

Example 6: Limiting Multiple Processes

Limit processes with PIDs 1234, 2345, and 3456 to 20% CPU usage each:

sudo cpulimit -p 1234 -l 20 -b &
sudo cpulimit -p 2345 -l 20 -b &
sudo cpulimit -p 3456 -l 20 -b &

Example 7: Limiting a Specific Command

To limit the CPU usage of a process, you can use the –pid option followed by the PID of the process and the –limit option followed by the percentage of CPU you wish to allocate to the process.

Let’s limit a ls command to 15% CPU usage:

sudo cpulimit -l 15 ls

Note: Use top, htop, or pidstat to monitor CPU usage after applying the limit. Experiment with different limit values to find the optimal setting for your system. Be aware that CPULimit might not provide exact CPU usage control. For more granular control over resource allocation, consider using cgroups.

Monitoring the Changes

After setting the limit, you can monitor the CPU usage of the process using the top command again to ensure that the limit is being enforced. The CPU usage should now reflect the percentage you specified.

top

Remember, Tools like ulimit, cgroups, and systemd-run can also be utilized to set limits on memory, disk I/O, and more, offering a comprehensive solution for resource control.

Conclusion

CPULimit is a valuable tool for managing CPU usage in Linux systems. By understanding its options and usage, you can effectively control resource consumption and improve overall system performance. CPULimit is a simple yet powerful tool for managing process resources on Linux systems.

By limiting CPU usage, you can ensure that no single process monopolizes the CPU, which could lead to system sluggishness or unresponsiveness. Whether you’re an experienced system administrator or a casual Linux user, understanding how to use CPULimit can help you maintain control over your system’s resources.

You can also explore List Running Process on Linux on Linux World.