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: a minus in front of a systemd ExecStartPre= command ignores its failure

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

[Service]

Let an optional preparation step fail without stopping the service from starting.

Any ExecStartPre= line that fails normally stops the whole start. For optional steps, like warming a cache or removing a stale lock file, that is too strict.

Put a - right after the equals sign and systemd treats a failure of that command as success:

[Service]
ExecStartPre=-/usr/bin/rm /run/myapp/stale.lock
ExecStartPre=/usr/local/bin/check-config
ExecStart=/usr/local/bin/myapp

Here the lock removal may fail when there is no lock, and that is fine. The config check has no prefix, so a broken config still stops the start. Even ExecStartPre=-/bin/false lets the service come up as active.

The minus is one of several prefixes in the man page’s table of special executable prefixes. @ sets argv[0], : turns off variable expansion, + runs the command with full privileges even when the unit uses User= or sandboxing. That last one is useful for a ExecStartPre=+ that has to chown a directory before a non-root service starts.

The same prefix works on ExecStart= itself, but I’d think twice there. A main process that always counts as successful hides real crashes from Restart=on-failure and from anything watching for failed units.

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