LinuxCommandLibrary

systemd-inhibit

Temporarily prevent system sleep/shutdown operations

TLDR

List all active inhibition locks and the reasons for their creation

$ systemd-inhibit --list
copy

Block system shutdown for a specified number of seconds with the sleep command
$ systemd-inhibit --what shutdown sleep [5]
copy

Keep the system from sleeping or idling until the download is complete
$ systemd-inhibit --what sleep:idle wget [https://example.com/file]
copy

Ignore lid close switch until the script exits
$ systemd-inhibit --what sleep:handle-lid-switch [path/to/script]
copy

Ignore power button press while command is running
$ systemd-inhibit --what handle-power-key [command]
copy

Describe who and why created the inhibitor (default: the command and its arguments for --who and Unknown reason for --why)
$ systemd-inhibit --who [$USER] --why [reason] --what [operation] [command]
copy

SYNOPSIS

systemd-inhibit [OPTIONS...] [COMMAND [ARGUMENTS...]]

PARAMETERS

-h, --help
    Displays a brief help message and exits.

--version
    Shows the version information of the systemd suite and exits.

--list
    Lists all currently active inhibition locks on the system.

--what=WHAT
    Specifies what actions to inhibit. Multiple values can be separated by colons, e.g., 'shutdown:sleep:idle'.

--who=WHO
    A short, descriptive string identifying who is requesting the inhibition, typically an application or user.

--why=WHY
    A human-readable reason for the inhibition, explaining why the lock is needed.

--mode=MODE
    Defines the inhibition mode: block (default, prevents action), delay (delays action for a short period), or kick (forces session termination).

--scope=SCOPE
    Sets the scope of the inhibition: user (default, tied to the user session via logind) or system (system-wide via systemd).

--terminate-on-close
    Ensures the inhibited COMMAND is terminated if its standard input, output, or error streams are closed.

--no-block
    Acquires the inhibition lock and then immediately exits. The lock remains active as long as the client process is running or its controlling TTY is open.

--kill-when-unused
    Releases the inhibition lock when the executed COMMAND exits or is terminated, even if systemd-inhibit was started with --no-block.

--dump
    Dumps the internal state of active inhibition locks in a machine-readable format.

DESCRIPTION

systemd-inhibit is a command-line utility used to execute a program while holding a system inhibition lock. This lock prevents critical system actions such as shutdown, sleep, idle, or the handling of power and lid-switch keys from occurring. It's particularly useful for ensuring uninterrupted operation during sensitive tasks like data backups, system updates, media burning, or presentations, where an unexpected system state change could lead to data loss or disruption.

When run with a COMMAND, systemd-inhibit acquires the specified inhibition lock(s) via the systemd-logind service and then executes the COMMAND. The lock is automatically released when the COMMAND terminates. If no COMMAND is provided, systemd-inhibit will hold the lock indefinitely until it is manually terminated, effectively blocking the specified actions until the process exits.

The command offers granular control over what is inhibited (--what), who is requesting the lock (--who), the reason for the lock (--why), and the behavior mode of the lock (--mode), such as block (default), delay, or kick. It also allows listing current active locks using the --list option.

CAVEATS

While systemd-inhibit is effective for preventing unintended system actions, it relies on the cooperative nature of systemd and its components like logind. It is not a security measure and can potentially be overridden by root privileges or direct D-Bus calls. Misuse or improper termination of the systemd-inhibit process or its child COMMAND (especially when using --no-block without --kill-when-unused properly) could lead to a persistent inhibition lock, preventing legitimate system power management actions until the lock is manually released or the associated process is terminated.

HISTORY

The systemd-inhibit command is an integral part of the systemd init system, which gained widespread adoption in Linux distributions as a replacement for SysVinit and Upstart. Its development reflects the modern approach to power management and session control on Linux, primarily managed through the systemd-logind service and D-Bus. It provides a standardized and robust mechanism for applications and scripts to temporarily prevent system-wide power-related actions, moving away from disparate or less integrated inhibition methods used in older Linux environments.

SEE ALSO

systemd(1), systemctl(1), logind.conf(5), systemd-logind.service(8), poweroff(8), reboot(8)

Copied to clipboard