How to setup a notification in case of systemd service failure?

Thank you for the encouragement and sorry for my late reply. However, I am still puzzled on some aspects and, ultimately, if the code snippet I had in mind in the first message would work.

First, you write:

There’s a service which may succeed or fail (btrfs-scrub)

You mention btrfs-scrub (which actually it was me who first mentioned it in my initial post). Nevertheless, I am not really sure that this is how the systemd service is named.

Do you know how can I find out the real name? According to this, I suspect the service name should be something like “btrfs-scrub-/”? Is it true?

On the light of what you wrote, I rewrote the script like the following:

systemd.services.btrfs-scrub-/.unitConfig.onFailure = [ "notify-failure@btrfs-scrub" ];
systemd.services."notify-failure@btrfs-scrub" = {
  enable = true;
  description = "Failure notification for %i";
  script = ''${pkgs.curl}/bin/curl -d  \
           --fail `#fail fast on server errors` \
           --show-error --silent `#show error <=> it fails` \
           --max-time 10 \
           --retry 3 \
           --data "%i exited with errors" ntfy.mydomain.com/mytopic'';
};

Will it work? Is there a way to test it? I don’t want to test my luck that btrfs scrub fails and hope it will work… :sweat_smile: