Alert on Bash error

Do you want to trigger an alert if a Bash script command fails its execution?

For a reusable alerting Bash script, take a look at our Helper function (Bash).

Example
#!/usr/bin/env bash
set -Eeumo pipefail
# Uncomment to follow the script process
# set -x

# Example: Compute and validate SHA256 checksums
# Expects N17_API_KEY as environment variable
function example() {
  # Make up the example's files
  echo "Hello!" > ./sha_example_1.txt
  echo "I will fail :(" > ./sha_example_2.txt
  
  # Compute the SHA checksums
  sha256sum ./sha_example_1.txt ./sha_example_2.txt > sha_list
  
  # Make the second file invalid for the example (alter the SHA checksum)
  sed -i '2 s/^./1/' sha_list
  
  # Verify the checksums (we expect the second file check to fail)
  # If the check fails, it will trigger an alert with the check results
  CHECK_RESULT=$(sha256sum -c sha_list) || { 
    curl "https://hook.notify17.net/api/raw/${N17_API_KEY}" \
      -F title="SHA256 check failed" \
      -F content=$'Result:\n\n'"$CHECK_RESULT"
        
    # We want to still break the program on failure!
    return 1
  }
  
  # Successful execution
  echo "Valid checksums!"
  return 0
}

# Run the example
example || echo "Oh! Something went wrong!"