How to Use the Command 'cpulimit' (with examples)

How to Use the Command 'cpulimit' (with examples)

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 directs cpulimit 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 the cpulimit 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 the cpulimit 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: Configures cpulimit 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.

Related Posts

How to Use the `ssh-agent` Command (with Examples)

How to Use the `ssh-agent` Command (with Examples)

The ssh-agent command is a key component within the SSH (Secure Shell) ecosystem, serving as an authentication agent that keeps your SSH private keys secure but ready for use.

Read More
How to use the command 'aws kendra' (with examples)

How to use the command 'aws kendra' (with examples)

AWS Kendra is a robust, fully managed enterprise search service powered by machine learning.

Read More
How to Use the Command 'osv-scanner' (with Examples)

How to Use the Command 'osv-scanner' (with Examples)

The osv-scanner command is a powerful tool used to analyze various software components for vulnerabilities.

Read More