Skip to content

Instantly share code, notes, and snippets.

@odolbeau
Last active February 7, 2025 16:49
Show Gist options
  • Save odolbeau/bd6d8eb7910d1289e2687682c8db9275 to your computer and use it in GitHub Desktop.
Save odolbeau/bd6d8eb7910d1289e2687682c8db9275 to your computer and use it in GitHub Desktop.
Healtchecks.io bash decorator
#!/bin/bash
# Hosted on gist : https://gist.github.com/odolbeau/bd6d8eb7910d1289e2687682c8db9275
if [ $# -lt 1 ] || [ -z "$1" ] || [ -z "${UUID}" ]; then
echo "Usage: UUID=your-uuid $0 [command to run]"
echo "Decorates a command to notify healtchecks.io"
exit 1
fi
curl -fsS -m 10 --retry 5 -o /dev/null "https://hc-ping.com/$UUID/start"
LOG_FILE=/tmp/healtchecks.io-$(echo "$@" | md5sum | head -c 20)-$(echo $RANDOM | md5sum | head -c 20).log
# Mandatory to not loose the program return code
# @see https://stackoverflow.com/a/6872163
set -o pipefail
"$@" | tee "$LOG_FILE"
RETURN_CODE=$?
set +o pipefail
# Retrieve the output from the log file to send it to healtchecks.io
OUTPUT=$(tail --bytes=100000 "$LOG_FILE")
curl -fsS -m 10 --retry 5 --data-raw "$OUTPUT" -o /dev/null "https://hc-ping.com/$UUID/$RETURN_CODE"
# If everything went fine, do not keep the log file
if [ 0 == $RETURN_CODE ]; then
rm "$LOG_FILE"
fi
exit $RETURN_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment