How to Use the Command 'cpulimit' (with examples)
- Linux
- December 17, 2024
The cpulimit
command is a useful tool designed to control the CPU usage of other processes on a Unix-based system. This utility is invaluable for system administrators and users who aim to manage their system’s performance by throttling processes that may be consuming excessive CPU resources. By setting limits on CPU usage, cpulimit
helps ensure that critical processes receive the necessary computational resources without being impeded by less important tasks. This is especially useful when dealing with resource-intensive applications or when attempting to maintain system responsiveness in a shared, multi-user environment.
Use case 1: Limit an existing process with PID 1234 to only use 25% of the CPU
Code:
cpulimit --pid 1234 --limit 25%
Motivation:
Limiting the CPU usage of a process with a specific Process ID (PID) allows you to control a process you know is consuming too much CPU. For instance, a background service or a runaway process might be over-utilizing CPU, slowing down other critical operations. Using cpulimit
, you can ensure it only consumes a reasonable percentage of the CPU, allowing other processes to work efficiently.
Explanation:
--pid 1234
: This option specifies the target process by its unique process identifier (PID), which in this case is 1234. The PID is an integer assigned by the operating system to identify running processes.--limit 25%
: This specifies that the CPU usage of the target process should be capped at 25%. This percentage represents the maximum allowable CPU load for the process, keeping its consumption under control.
Example Output:
After running this command, the CPU usage of the process with PID 1234 will be reduced and limited to 25%, as reflected in system monitoring tools like top
or htop
.
Use case 2: Limit an existing program by its executable name
Code:
cpulimit --exe program --limit 25
Motivation:
Sometimes, you may want to limit a program’s CPU usage without knowing its PID but only its executable name. This is ideal when you want to apply constraints to all instances of the program running across your system, ensuring consistent CPU usage across multiple processes initiated by the same executable.
Explanation:
--exe program
: This argument indicates the program by its executable name (e.g., program). It directscpulimit
to target all processes associated with this executable.--limit 25
: Similar to before, this limits the CPU usage to 25%. Not specifying the percentage sign implies the same intention of capping the CPU usage to 25% of a single-core CPU.
Example Output:
Each process launched from the ‘program’ executable will have its CPU usage maintained at or below 25%. Observant users will note this in resource monitors by checking multiple instances of the application.
Use case 3: Launch a given program and limit it to only use 50% of the CPU
Code:
cpulimit --limit 50 -- program argument1 argument2 ...
Motivation:
There are scenarios where you need to manage CPU usage right from the launch of a new application. Limiting CPU usage as you start a program is crucial when the anticipated process can be computationally intensive, such as data processing tasks or simulations.
Explanation:
--limit 50
: Sets the CPU usage cap for the upcoming process to 50%.-- program argument1 argument2 ...
: This section launches the desired program with its corresponding arguments. All arguments passed after--
are associated with the program you want to execute.
Example Output:
The program initiated will run with the predefined CPU restrictions, immediately observable in system tools showing its capped utilization.
Use case 4: Launch a program, limit its CPU usage to 50% and run cpulimit in the background
Code:
cpulimit --limit 50 --background -- program
Motivation:
Background processing is essential in multitasking environments. Frequently, users need to launch applications suited for long computations or background tasks without occupying the terminal. By running cpulimit
in the background, users can continue other tasks on their workstation without patiently waiting for the limit-setting process to complete.
Explanation:
--limit 50
: Specifies the CPU usage limit at 50%, similar to above.--background
: This makes thecpulimit
process run in the background, freeing up the terminal for other commands while the program executes under the specified limits.-- program
: Follows the same logic of initiating the desired program as seen previously but with thecpulimit
process not blocking the terminal.
Example Output:
After executing the command, the terminal is available for other inputs, and the program will adhere to the CPU constraints imposed by cpulimit
.
Use case 5: Kill its process if the program’s CPU usage goes over 50%
Code:
cpulimit --limit 50 --kill -- program
Motivation:
Immediately terminating a process that exceeds a defined CPU usage threshold can be necessary to maintain system stability, especially on production servers where resource spikes could impact uptime and service responsiveness.
Explanation:
--limit 50
: Sets the threshold for CPU usage at 50%.--kill
: Configurescpulimit
to automatically kill the process when its CPU consumption surpasses the defined threshold. This is an aggressive measure to prevent runaway processes from affecting critical operations.-- program
: Denotes the executable you wish to apply this strict control over.
Example Output:
Upon exceeding the 50% CPU usage, the process will be terminated, which will be noticeable as it disappears from process managers and resource monitoring tools instantly.
Use case 6: Throttle both it and its child processes so that none go about 25% CPU
Code:
cpulimit --limit 25 --monitor-forks -- program
Motivation:
Complex applications often spawn multiple child processes. Applying CPU usage limits only to the parent process, while leaving children unrestricted, can lead to unintended high resource utilization. Limiting both parent and child processes equally ensures a cumulative CPU resource control, preserving system balance.
Explanation:
--limit 25
: The CPU cap remains consistent for each process spawned by the parent.--monitor-forks
: Activates monitoring and controlling of both the main process and all its child processes, preventing any from exceeding the specified CPU limit.-- program
: Initiates the primary process, intended for CPU management alongside its spawned tasks.
Example Output:
Running this command ensures that all spawned processes maintain CPU usage below the 25% limit, verifying their compliance via resource management utilities.
Conclusion:
The cpulimit
command provides users with the flexibility to manage CPU resources effectively. Its versatility through various command options allows for unique control, catering to specific user needs — from limiting standalone applications to handling complex multi-process scenarios. Understanding these use cases ensures optimized system performance and prevents resource contention, contributing to the overall stability and efficiency of your computational environment.