Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rcj4747
Last active May 14, 2021 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rcj4747/ec45687b860cd4e5b718e8764e67d436 to your computer and use it in GitHub Desktop.
Save rcj4747/ec45687b860cd4e5b718e8764e67d436 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)
@rcj4747
Copy link
Author

rcj4747 commented May 14, 2021

I use this in my crontab to wrap jobs. The tools like this in the docs @ https://healthchecks.io/docs/resources/ all were trying to do too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment