LinuxCommandLibrary

cpulimit

Limit CPU usage of a process

TLDR

Limit an existing process with PID 1234 to only use 25% of the CPU

$ cpulimit [[-p|--pid]] [1234] [[-l|--limit]] [25%]
copy

Limit an existing program by its executable name
$ cpulimit [[-e|--exe]] [program] [[-l|--limit]] [25]
copy

Launch a given program and limit it to only use 50% of the CPU
$ cpulimit [[-l|--limit]] [50] -- [program argument1 argument2 ...]
copy

Launch a program, limit its CPU usage to 50% and run cpulimit in the background
$ cpulimit [[-l|--limit]] [50] [[-b|--background]] -- [program]
copy

Kill its process if the program's CPU usage goes over 50%
$ cpulimit [[-l|--limit]] 50 [[-k|--kill]] -- [program]
copy

Throttle both it and its child processes so that none go about 25% CPU
$ cpulimit [[-l|--limit]] [25] [[-m|--monitor-forks]] -- [program]
copy

SYNOPSIS

cpulimit [OPTIONS] { -p PID | -e PROGRAM | -c COMMAND_LINE }
cpulimit [OPTIONS] -- PROGRAM [ARGUMENTS]

PARAMETERS

-l PERCENT
    Set the CPU limit percentage (e.g., 50 for 50% CPU).

-p PID
    Target process by its Process ID.

-e PROGRAM
    Target process by its executable name.

-c COMMAND_LINE
    Target process by its full command line.

-k
    Kill the process if it exceeds the limit for too long (requires -l).

-r
    Reset statistics after the limit has been reached, making it restart CPU calculation for a new period.

-z
    Exit cpulimit if the target process dies or is not found.

-b
    Run cpulimit in the background as a daemon.

-v
    Enable verbose output, showing monitoring details.

--
    Separator used when specifying a new program and its arguments to launch and limit.

DESCRIPTION

The cpulimit command is a utility designed to control the CPU usage of a specific process. It achieves this by sending SIGSTOP and SIGCONT signals to the target process, effectively pausing and resuming its execution to maintain its CPU consumption below a defined percentage. This is particularly useful for managing runaway processes, preventing a single application from monopolizing system resources, or ensuring fair CPU distribution among multiple tasks on a shared system. Unlike resource management through cgroups, cpulimit operates on a per-process basis and does not require kernel-level configuration. It can target processes by their Process ID (PID), executable name, or full command line, and can also launch and control new processes.

CAVEATS

While effective for general CPU limiting, cpulimit has limitations.

It's not suitable for processes requiring precise real-time performance due to the nature of SIGSTOP/SIGCONT signals introducing latency. It may also struggle with very short-lived processes or those with highly bursty CPU usage patterns. As it works by pausing execution, it doesn't prevent a process from requesting high CPU, only from consuming it. For more robust and fine-grained resource control, Linux cgroups are generally preferred.

HOW IT WORKS

cpulimit continuously monitors the CPU usage of its target process. If the process's CPU consumption exceeds the specified percentage, cpulimit sends a SIGSTOP signal to pause it. Once the usage falls below the threshold (or after a calculated pause duration), it sends a SIGCONT signal to resume the process. This cycle repeats to maintain the desired CPU limit.

COMMON USE CASES

Controlling CPU-intensive tasks like video encoding, large compilation jobs, or scientific simulations to prevent system slowdowns. Limiting browser processes or Electron-based applications that might consume excessive CPU. Ensuring background batch jobs do not impact interactive user experience. Preventing runaway scripts or applications from monopolizing CPU resources.

HISTORY

The cpulimit utility was developed as a straightforward alternative to more complex resource management tools like cgroups, focusing on an easy-to-use command-line interface for individual process CPU throttling. Its initial release was around 2007, providing a practical solution for common desktop and server scenarios where simple CPU limiting was required without extensive system configuration.

SEE ALSO

nice(1), renice(1), cgroups(7), systemd-run(1), kill(1)

Copied to clipboard