How to use the command 'cpulimit' (with examples)

How to use the command 'cpulimit' (with examples)

The cpulimit command is a tool used to throttle the CPU usage of other processes. It allows you to limit the CPU usage of specific processes or programs, ensuring that they do not consume all available CPU resources. This can be useful in scenarios where certain processes or programs are causing high CPU usage, leading to system slowdowns or instability.

Use case 1: Limit an existing process with PID 1234 to only use 25% of the CPU

Code:

cpulimit --pid 1234 --limit 25%

Motivation: In situations where a specific process is utilizing a significant amount of CPU resources and causing performance issues, you can limit its CPU usage to mitigate the impact it has on other processes and system performance.

Explanation:

  • --pid 1234: Specifies the process ID (PID) of the target process you want to limit.
  • --limit 25%: Sets the CPU usage limit to 25%.

Example output: The specified process with PID 1234 will now be limited to utilizing only 25% of the CPU.

Use case 2: Limit an existing program by its executable name

Code:

cpulimit --exe program --limit 25

Motivation: Instead of identifying a process by its PID, this use case allows you to limit the CPU usage of a specific program identified by its executable name. This can be helpful when you know the name of the program causing high CPU usage but not its PID.

Explanation:

  • --exe program: Specifies the name of the target program you want to limit.
  • --limit 25: Sets the CPU usage limit to 25%.

Example output: The program named “program” will now be limited to utilizing only 25% of the CPU.

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: This use case allows you to launch a specific program and immediately limit its CPU usage. It can be useful when you want to start a program and ensure it does not consume all available CPU resources from the beginning.

Explanation:

  • --limit 50: Sets the CPU usage limit to 50%.
  • --: Separates the cpulimit options from the program and its arguments.
  • program argument1 argument2 ...: Specifies the program name and its accompanying arguments.

Example output: The program will be launched and limited to utilizing only 50% of the CPU.

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: This use case is similar to the previous one, but it additionally runs cpulimit in the background, allowing you to continue using the terminal while the program is running and its CPU usage is limited.

Explanation:

  • --background: Runs cpulimit in the background.
  • --limit 50: Sets the CPU usage limit to 50%.
  • --: Separates the cpulimit options from the program.

Example output: The program will be launched and limited to utilizing only 50% of the CPU, while cpulimit runs in the background.

Use case 5: Kill its process if the program’s CPU usage goes over 50%

Code:

cpulimit --limit 50 --kill -- program

Motivation: This use case allows you to automatically terminate a program if its CPU usage exceeds a specified limit. It can be useful in situations where you want to prevent a certain program from consuming excessive CPU resources and causing system performance issues.

Explanation:

  • --kill: Instructs cpulimit to kill the process if its CPU usage exceeds the specified limit.
  • --limit 50: Sets the CPU usage limit to 50%.
  • --: Separates the cpulimit options from the program.

Example output: If the CPU usage of the program exceeds 50%, cpulimit will terminate the process.

Use case 6: Throttle both it and its child processes so that none go above 25% CPU

Code:

cpulimit --limit 25 --monitor-forks -- program

Motivation: In some cases, limiting the CPU usage of just the main program is not enough, as it may spawn child processes that also consume significant CPU resources. This use case allows you to throttle both the main program and its child processes, ensuring that none of them exceed the specified CPU usage limit.

Explanation:

  • --limit 25: Sets the CPU usage limit to 25% for both the main program and any child processes it spawns.
  • --monitor-forks: Instructs cpulimit to monitor and throttle all child processes as well.
  • --: Separates the cpulimit options from the program.

Example output: Both the main program and any child processes spawned by it will be limited to utilizing a maximum of 25% of the CPU.

Related Posts

How to use the command 'peco' (with examples)

How to use the command 'peco' (with examples)

peco is an interactive filtering tool that allows users to filter inputs interactively.

Read More
Montage Command Examples (with examples)

Montage Command Examples (with examples)

Use Case 1: Tile images into a grid, automatically resizing images larger than the grid cell size montage path/to/image1.

Read More
How to use the command pactl (with examples)

How to use the command pactl (with examples)

Pactl is a command-line tool used to control a running PulseAudio sound server.

Read More