basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    OnFailure= in systemd services: alert when a service fails

    When a system service crashes and cannot restart, systemd puts it into the failed state and tells no one. An OnFailure= line in [Unit] triggers a template notifier unit that sends the failed service’s name and the tail of its journal right away.

    When a service like crowdsec-firewall-bouncer or a background timer fails, systemd marks the unit failed and stops there. Until someone logs in and runs systemctl --failed, nothing signals the outage. OnFailure= hooks the transition to failed onto another unit — usually a single shared template that sends an alert to ntfy or email.

    OnFailure= does not fire on a manual systemctl stop or a clean exit with code 0. If a service has Restart=on-failure, systemd first tries to restart it (by default up to 5 times in 10 s, controlled by StartLimitBurst), and only after exhausting retries does it enter failed and trigger the notifier.

    One template for every service

    Create the template unit /etc/systemd/system/[email protected] — the @ in the name passes the failed unit’s name as %i. Read the ntfy topic from /srv/secrets/ntfy/topic, created during the monitoring setup (Metrics, uptime checks and alerts):

    # /etc/systemd/system/[email protected]
    [Unit]
    Description=Failure notification for %i
    After=network-online.target
    OnFailure=
    
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c 'topic=$(cat /srv/secrets/ntfy/topic); log=$(journalctl -u "$1" -n 15 --no-pager -o short-iso); curl -fsS -H "Title: $(hostname): $1 failed" -H "Priority: high" -d "$log" "https://ntfy.sh/$topic"' _ %i
    • Type=oneshot makes systemd wait for curl to finish and record in the journal whether delivery succeeded.
    • _ %i passes the service name to /bin/sh -c as $1, so special characters in a unit name cannot break the command string.
    • An empty OnFailure= in [Unit] prevents recursion if you later add a global drop-in for all services.

    Attach it to a service via a drop-in

    Leave packaged unit files in /lib/systemd/system/ untouched — upgrades overwrite them. Add a drop-in in the service’s .service.d/ directory, for example for the CrowdSec bouncer:

    sudo install -d /etc/systemd/system/crowdsec-firewall-bouncer.service.d
    sudo tee /etc/systemd/system/crowdsec-firewall-bouncer.service.d/onfailure.conf > /dev/null <<'EOF'
    [Unit]
    OnFailure=notify-failure@%n.service
    EOF
    sudo systemctl daemon-reload

    The %n specifier expands to the current unit’s full name (crowdsec-firewall-bouncer.service). Drop the exact same two-line file into any other service’s .service.d/ without editing the name inside.

    To cover every service on the host at once, place that same two-line file at /etc/systemd/system/service.d/10-onfailure.conf (supported since systemd 239). The empty OnFailure= in [email protected] is mandatory here, or a network outage makes the notifier trigger itself in a loop.

    What OnFailure= does not replace

    The notifier runs on the same server where the service failed. If the VPS itself goes down, networking drops, or a disabled timer never starts a backup, OnFailure= stays silent. Use OnFailure= for immediate alerts on crashed system services with journal context, and keep an external silence-based watchdog for scheduled jobs and host liveness (Alert on silence, Healthchecks.io).

    How to check

    First test the template directly against an existing service:

    sudo systemctl start [email protected]

    Your phone should receive an ntfy alert titled web1: crowdsec-firewall-bouncer.service failed with the last 15 lines of its journal.

    Next test an actual failure by running a transient unit that exits non-zero right away:

    sudo systemd-run --unit=test-fail -p [email protected] /bin/false
    sudo systemctl reset-failed test-fail.service

    The first command puts test-fail.service into failed and triggers the notifier; an alert lands in ntfy. The second clears the temporary failure from systemctl --failed.

    If it doesn’t work

    The service entered failed, but no notification arrived in ntfy

    Cause
    You forgot daemon-reload after adding the .service.d/ file, or put OnFailure= under [Service] instead of [Unit].
    Fix
    Check systemctl show -p OnFailure <service>: it should print OnFailure=notify-failure@<service>.service. If it is empty, move the directive to [Unit] and run sudo systemctl daemon-reload.

    The service crashed and restarted, but no alert fired

    Cause
    With Restart=on-failure or Restart=always, the service enters auto-restart, not failed. OnFailure= fires only when the service settles in failed (for instance, after hitting StartLimitBurst).
    Fix
    That is expected: a one-off glitch that recovers stays quiet, while a stuck service raises an alert.

    notify-failure@… itself fails while sending the alert

    Cause
    The notifier runs as a non-root user and cannot read /srv/secrets/ntfy/topic, or the service failed during boot before networking came up.
    Fix
    Check the cause in sudo journalctl -u 'notify-failure@*' -n 20. Keep the notifier running as root with After=network-online.target.

    After adding a global drop-in in /etc/systemd/system/service.d/, notifications fail in a loop during a network outage

    Cause
    The global rule applied to [email protected] itself, so its own failure triggers it again.
    Fix
    Add an empty OnFailure= line under [Unit] in [email protected] to reset inheritance.