Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run a command and report status to https://healthchecks.io
#!/bin/bash -eu
set -o pipefail
# Run a command and report status to https://healthchecks.io
usage() {
echo "Usage: $0 <healthchecks.io UUID> <command>"
exit 1
}
trap usage EXIT
URL="https://hc-ping.com/${1:?No UUID provided}"
shift
trap - EXIT
OUTPUT=""
healthcheck() {
# Log output is limited to 10,000 bytes, so let's send the tail
# as that's likely more useful for debugging
OUTPUT=$(echo "${OUTPUT}" | tail --bytes=10000 -)
curl \
--fail --silent --show-error \
--retry 5 -o /dev/null \
--data-raw "${OUTPUT}" \
$1
}
report_start() {
healthcheck "${URL}/start"
}
report_rc() {
RC=$?
healthcheck "${URL}/${RC}"
exit ${RC}
}
trap report_rc EXIT
healthcheck "$URL/start"
# Run command and store output in $OUTPUT for logging at healthcheck.io
OUTPUT=$($@ 2>&1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment