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: RestartSteps= makes systemd wait longer between each restart

Systemd
~/tips/tip-systemd-restart-steps

[Service]

Back off gradually when a service keeps crashing instead of hammering it every few seconds.

Restart=on-failure with a fixed RestartSec= retries at the same pace forever. When a database is down for an hour, that is a lot of pointless attempts and log lines.

Since systemd 254 you can let the delay grow:

[Service]
Restart=on-failure
RestartSec=2
RestartSteps=5
RestartMaxDelaySec=5min

The first restart waits 2 seconds. With each further restart the delay grows, reaching 5 minutes after five steps, and stays there. For a unit whose ExecStart=/bin/false fails right away, systemctl show flaky -p RestartUSecNext already reports almost 4 seconds for the next wait after two restarts.

To see where a service is in its back off:

systemctl show myapp -p NRestarts -p RestartUSecNext

I find this nicer than hitting the start limit, because the service keeps trying and comes back on its own once the thing it depends on returns.

Mind the start rate limit. With short delays, StartLimitBurst= may still trip first and leave the unit failed. Raise StartLimitIntervalSec= or set it to 0 if you rely on the back off. And on older systems like Ubuntu 22.04 the settings are unknown, so systemd logs a warning and ignores them.

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