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: ExecCondition= lets a script decide whether a systemd service runs

Systemd
~/tips/tip-systemd-exec-condition

[Service]

Skip a service quietly when a check command says now is not the time.

The Condition...= settings cover files, hosts and virtualization. When a condition is more complicated, like whether a backup disk has enough room, I’d rather write it as a small script.

ExecCondition= runs a command before ExecStartPre=. Exit code 0 means go on. An exit code from 1 to 254 makes systemd skip the rest without marking the unit as failed:

[Service]
Type=oneshot
ExecCondition=/bin/sh -c 'mountpoint -q /mnt/backup'
ExecStart=/usr/local/bin/backup.sh

With ExecCondition=/bin/sh -c "exit 1", systemctl start returns success, the unit stays inactive, and the log says Skipped due to 'exec-condition'. Nothing ends up in systemctl --failed.

That makes it a nice fit for timer jobs that should only run in certain situations: on AC power, when a NAS answers, when there is new data. The timer fires, the check says no, and nobody gets a failure mail.

Exit code 255, or a crash of the check itself, counts as a real failure. So a script that dies on a typo looks different from one that says no, which is good. The setting arrived in systemd 243, so very old servers ignore it with a warning and run the service anyway.

Further reading: systemd.service man 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