How to Prevent System Sleep and Shutdown in Ubuntu Using systemd-inhibit

Automated power management is an essential feature of modern Ubuntu workstations and cloud instances, putting machines to sleep after idle periods to conserve electrical power and protect hardware longevity. However, when executing multi-hour data backups, operating system release upgrades, DVD optical burns, or massive machine learning compilation jobs, automated system sleep, idle suspension, or accidental shutdowns can corrupt files and abort long-running tasks.

To manage this challenge cleanly, Ubuntu utilizes systemd-inhibit. Part of the systemd suite, systemd-inhibit allows processes and shell scripts to request temporary “inhibitor locks” from the login and power management daemon (systemd-logind), preventing sleep, idle timeouts, or shutdown events until the critical operation finishes.

Understanding systemd Inhibitor Lock Types

Inhibitor locks target specific power events known as “operations” (--what):

  • sleep: Blocks system suspend and hibernation.
  • shutdown: Blocks machine power-off and system reboot commands initiated by unprivileged users or automated schedulers.
  • idle: Prevents the system from declaring an idle state (blocking automatic screen locks, screensavers, and power-saving sleep).
  • handle-power-key: Inhibits actions bound to pressing the physical hardware power button.
  • handle-lid-switch: Inhibits automated suspension triggered by closing a laptop lid.

Running Commands with Sleep and Idle Inhibition

To execute a command while blocking automated suspension and idle timeout:

systemd-inhibit --what=sleep:idle --why="Running automated database backup" ./backup-database.sh

In this command:

  • --what=sleep:idle specifies that both system sleep and idle state timeouts are blocked.
  • --why="..." provides an informative text explanation registered with the systemd daemon, visible in system status reports.
  • ./backup-database.sh is the target command. Once the script exits—whether successfully or with an error—systemd automatically releases the lock, restoring normal power management.

Preventing Shutdown During System Maintenance

When compiling code from source or upgrading critical production services where an accidental reboot would cause significant downtime, inhibit shutdown operations:

systemd-inhibit --what=shutdown --mode=block --why="Kernel compilation in progress" make -j$(nproc)

The --mode=block parameter instructs the system to completely refuse shutdown requests. An attempt to power down the machine via the desktop interface or an unprivileged systemctl poweroff will be refused until the command terminates.

Querying Active Inhibitor Locks

To audit all inhibitor locks currently active on your Ubuntu system:

systemd-inhibit --list

The output prints a structured tabular list displaying:

  • Who: The user, application process, and PID holding the lock (e.g. GNOME Shell, apt, or a custom script).
  • UID: The user ID that requested the lock.
  • What: The targeted power operations inhibited.
  • Why: The descriptive explanation registered by the application.
  • Mode: Whether the lock is in block mode (hard refusal) or delay mode (delays the action up to a configured timeout).

Integrating Inhibitor Locks into Shell Scripts

You can embed inhibitor locks directly into your administrative maintenance scripts to safeguard operational execution:

#!/usr/bin/env bash
set -euo pipefail

echo "Acquiring power lock and starting file synchronization..."

systemd-inhibit \
    --who="Nightly Sync Script" \
    --what="sleep:shutdown" \
    --why="Mirroring 500GB dataset to offsite storage" \
    --mode=block \
    rsync -avz /data/ /mnt/backup/

echo "Synchronization complete. Inhibitor lock released automatically."

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.