[Solved] cURL script in pre & posthook options of borgbackup gives an error which fails the systemd service

I have setup borgbackup on my nixos server and it works flawlessly.

However, despite being successful, the corresponding systemd service ends as failed because of the ping via curl to the healthchecks.io website:

By the way, both the healthchecks.io pre and posthook urls are also successfully pinged.

This is an example of my preHook and postHook section:

    preHook = "${pkgs.curl}/bin/curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/blablabla1";
    postHook = "
      if [ $exitStatus -ne 0 ]; then
        ${pkgs.curl}/bin/curl \
             --fail --show-error --silent \
             --max-time 10 --retry 5 \
             --data Borgbackup_failed ntfy.mydomain.com/Borgbackup \
      else
        ${pkgs.curl}/bin/curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/blablabla2 \
      fi
    ";

Thus, if I comment the above part, the systemd service ends as not failed.

Do you know how can I solve this?

I mean, you can prevent the exit code from bubbling up to systemd by adding || true to the end of the line. That’ll swallow the non-zero exit code and continue as normal.

Personally I’d figure out why curl struggles with those URLs though. Does quoting them with ' help?

Edit:

Ah, no, you probably just need to remove the \ on the second one, it’s probably treating the else and fi as URLs.

2 Likes

Thank you! That was the reason! I could feel it was something very stupid… :confused: