# Trigger a command on error or failure

**URL:** <https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636>\
**Category:** Support\
**Created:** [January 8, 2025, 5:34am UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636 "2025-01-08T05:34:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tylerdurden4285](https://yyz2.discourse-cdn.com/free1/user_avatar/kopia.discourse.group/tylerdurden4285/32/1346_2.png) [@tylerdurden4285](https://kopia.discourse.group/u/tylerdurden4285)\
**Post date:** [January 8, 2025, 5:34am UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636/1 "2025-01-08T05:34:00Z")

</div>

I want to run a curl command like “curl -d “snapshot error” [https://notify.example.com/test](https://notify.example.com/test)” but only when there is an error or failure to take snapshot. I ran a test for a successful snapshot and it works fine, I receive the notification - how to only run it when there’s an error with kopia ui?

Edit: ChatGPT says do this, but I’m not sure?

```auto
[$? -ne 0] && curl -d "snapshot failed" https://notify.example.com/test

```

Edit: After successfully failing it didn’t work, so I think that this part is wrong. any advice?

```auto
[$? -ne 0] &&

```

---

<div class="post-metadata">

**Author:** ![IcePic](https://avatars.discourse-cdn.com/v4/letter/i/74df32/32.png) [@IcePic](https://kopia.discourse.group/u/IcePic)\
**Post date:** [January 8, 2025, 12:57pm UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636/2 "2025-01-08T12:57:00Z")

</div>

What it suggested should work, IF kopia sets the result code correctly. It is a bit long-winded way to first run kopia, then test the result code for non success, then use the “run only if successful” that looks at the test exist code and if the test was ok (because previous command was not), only then run the curl command. So, the advice is sort-of correct from a shell perspective, but it would have been simpler to just have

`kopia <commands and options> || curl -d ...`  
which means “run second command if first failed” instead of using && that only runs if the previous worked, and have that one inverted from the kopia result code.

Now, someone needs to check if kopia does set result codes correctly or not, and my guess is that it might not do that, but if it does, then || is easier I guess.

---

<div class="post-metadata">

**Author:** ![tylerdurden4285](https://yyz2.discourse-cdn.com/free1/user_avatar/kopia.discourse.group/tylerdurden4285/32/1346_2.png) [@tylerdurden4285](https://kopia.discourse.group/u/tylerdurden4285)\
**Post date:** [January 8, 2025, 1:11pm UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636/3 "2025-01-08T13:11:51Z")

</div>

So based on what you said in the “after snapshot” of the policy in the UI I should put the following?

```auto
|| curl -d "snapshot failed" https://notify.example.com/test

```

 ![image](https://global.discourse-cdn.com/free1/uploads/kopia/original/2X/f/fbe3c6a9b4678969e076623c42e1e12967589820.png)

I’m fine with adding a command before/after snapshots or files since it allows me to do either one, but I just don’t know what to put so it triggers ONLY on failure/error. Like I said it works great on successful completion of snapshot.

---

<div class="post-metadata">

**Author:** ![IcePic](https://avatars.discourse-cdn.com/v4/letter/i/74df32/32.png) [@IcePic](https://kopia.discourse.group/u/IcePic)\
**Post date:** [January 8, 2025, 4:41pm UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636/4 "2025-01-08T16:41:22Z")

</div>

No, it was based on if you run kopia from command-line or a script, not what to type in a gui window, since that “command” will most probably not be run in a context where it can read out the previous kopia run exit/return code.  
The list of informational environment variables listed here (for the After) actions should need one that says if the snap was successful or not, and as of now it doesn’t seem to have any such indicator:

> **[Actions](https://kopia.io/docs/advanced/actions/#action-environment)**
>
> Fast and Secure Open-Source Backup Software for Windows, Mac, and Linux

---

<div class="post-metadata">

**Author:** ![tylerdurden4285](https://yyz2.discourse-cdn.com/free1/user_avatar/kopia.discourse.group/tylerdurden4285/32/1346_2.png) [@tylerdurden4285](https://kopia.discourse.group/u/tylerdurden4285)\
**Post date:** [January 9, 2025, 1:25am UTC](https://kopia.discourse.group/t/trigger-a-command-on-error-or-failure/3636/5 "2025-01-09T01:25:47Z")

</div>

For anyone interested in following along I’ve created a git issue on this as I’ve now asked in the forum and on reddit as well as checked stackoverflow and chatgpt and cannot find any solutions to what I imagine would be a common requirement of knowing when snapshots fail for Kopia’s GUI version.

[https://github.com/kopia/kopia/issues/4340](https://github.com/kopia/kopia/issues/4340)
