Read more

Desktop notification for failed systemd user services

Andreas Vöst
January 10, 2023Software engineer at makandra GmbH

You can execute systemctl --user --failed to check for failed systemd user units. But let's face it: It's inconvenient and you'll probably miss failures. Better use desktop notifications.

~/.config/systemd/user/service.d/user-failure-notification.conf

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Add a OnFailure handler to all user units.

[Unit]
OnFailure=user-failure-notification@%n

~/.config/systemd/user/user-failure-notification@.service

Add a template service unit which sends the notifications. The instance variable %i is replaced by the calling unit.

[Unit]
Description=Send a notification about a failed systemd user unit

[Service]
ExecStart=/usr/bin/notify-send -u critical -a systemd "Failed unit: %i"

Activate

systemctl --user daemon-reload

Check

Run systemctl --user cat $unit to verify the config:

$ systemctl --user cat pipewire.service

# /usr/lib/systemd/user/pipewire.service
[Unit]
Description=PipeWire Multimedia Service
Requires=pipewire.socket
ConditionUser=!root

[Service]
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
RestrictNamespaces=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
Type=simple
ExecStart=/usr/bin/pipewire
Restart=on-failure
Slice=session.slice

[Install]
Also=pipewire.socket
WantedBy=default.target

# /home/$user/.config/systemd/user/service.d/user-failure-notification.conf
[Unit]
OnFailure=user-failure-notification@%n
Posted by Andreas Vöst to makandra Operations (2023-01-10 17:24)