Ping CronMissed from a shell script

On this page

In a shell script, one curl line after the job is the whole integration. This wrapper adds the start ping, sends the exit status, attaches the end of the output and makes sure a lost ping never changes what the script returns.

What should a shell job send?

  • A request to /start before the work, so CronMissed can time the run and notice a job that never finishes.
  • A request to the plain URL when the work succeeded, or to /fail when it did not. /<exit status> works too: 0 is a success and any other number is a failure.
  • The end of the output in the request body, up to 100,000 bytes, so the alert shows the error without a trip to the server.

What is the wrapper?

ping-wrapper.sh
#!/usr/bin/env bash
set -uo pipefail
PING_URL="${CRONMISSED_PING_URL:?set CRONMISSED_PING_URL}"

ping() { curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "$@" || true; }

ping "$PING_URL/start"
output=$(/usr/local/bin/job.sh 2>&1)
status=$?
printf '%s\n' "$output" | tail -c 100000 | ping --data-binary @- "$PING_URL/$status"
exit "$status"

Keep the ping URL in an environment variable or a secret store, never in a public repository: the URL is the check's only credential.

What do the curl flags do?

  • -f makes curl fail on an HTTP error status, -sS hides the progress meter but still prints errors.
  • --max-time 10 gives up after ten seconds so a slow network cannot hold the job.
  • --retry 5 retries on transient errors, and --retry-connrefused includes a refused connection among them.
  • || true keeps a failed ping from changing the script's own exit status.

The one-line form for a crontab is command && curl -fsS --retry 5 <url>. See the crontab guide.

Sources

Questions and answers

Why use HTTPS and not HTTP?
Plain HTTP requests are answered with a redirect to HTTPS, and a client that does not follow redirects loses the ping. Use the https:// address from the check page.
Why tail the output?
A ping body keeps up to 100,000 bytes. Sending the end keeps the request small and holds the part of the log where the error usually is.
Create the check these snippets report to

Make a check, copy its ping URL into the snippet, and run the job once.

Start monitoring