Skip to content

Instantly share code, notes, and snippets.

@philroche
Forked from rcj4747/healthchecks.sh
Created May 14, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philroche/108ed10b340740e64351421fb1811a62 to your computer and use it in GitHub Desktop.
Save philroche/108ed10b340740e64351421fb1811a62 to your computer and use it in GitHub Desktop.
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