brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: systemd-inhibit keeps a long job from being cut off by sleep or shutdown

Systemd
~/tips/tip-linux-systemd-inhibit-block-sleep

systemd-inhibit --why="Copying photos" rsync -a /mnt/user/photos/ /mnt/backup/photos/

Runs a command while preventing the machine from sleeping or shutting down.

A laptop or mini PC that suspends in the middle of a long copy is annoying, and the fix does not need any change in the power settings.

systemd-inhibit runs a command and holds an inhibitor lock for as long as it runs. The lock is acquired before the command starts and released afterwards. Without options it blocks idle:sleep:shutdown:

systemd-inhibit --why="Copying photos" rsync -a /mnt/user/photos/ /mnt/backup/photos/

The --why text is the reason other programs show when they see the lock, and the default is “Unknown reason”. --who says who holds the lock and defaults to the command line. You can narrow what is blocked with --what, which takes a colon-separated list:

systemd-inhibit --what=sleep:idle --why="Long copy" rsync -a /mnt/user/photos/ /mnt/backup/photos/

To see what is holding things back on a machine, list every active lock with its reason:

systemd-inhibit --list

The default mode is block, which prohibits the requested operations without time limit. That includes a shutdown you ask for yourself, until the command ends. If you want a privileged user to be able to override it, look at --mode=block-weak, which is not enforced against operations started by privileged clients. The third mode, delay, only holds back sleep and shutdown for a limited time.

The lock only works on a system that runs systemd, and it does nothing against a hard power-off, a pulled plug or a crash.

Further reading: systemd-inhibit manual page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link