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 OnFailure= can start a notify unit when a service fails

Systemd
~/tips/tip-systemd-onfailure-notify-template

OnFailure=notify@%n.service

Triggers a notification unit automatically whenever another service ends up failed.

I want a message the moment a service fails and not when I next look at the logs.

# myapp.service, [Unit]
OnFailure=notify@%n.service
# [email protected], [Service]
ExecStart=/usr/local/bin/notify.sh "%i failed"

OnFailure= starts the listed unit when the service enters the failed state. The template name [email protected] receives the failed unit’s name as the instance: %n expands to the full name of the failing unit, and %i is the instance in the template. Put a curl call to ntfy or a mail command in notify.sh. It does not fire for a clean stop. If Restart= is set, it fires only after the start limit is reached.

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