5.5Send pings from cron
cron is the program that runs jobs on a schedule on Linux and macOS. These examples use curl with the same options as the dashboard snippets:
| Option | What it does |
|---|---|
-f | Treat an HTTP error status as a failure. |
-sS | Hide progress output, but still show errors. |
-m 10 | Give up after 10 seconds. |
--retry 3 | Retry up to 3 times on temporary errors. |
A ping that fails must never fail your job. In these examples, a failed ping is ignored and the job still runs.
Simplest: ping on success#
Put the ping after the job with &&. && runs the next command only if the one before it succeeded.
PING_URL=https://<your-ping-url>
*/5 * * * * /usr/local/bin/sync-queue.sh && curl -fsS -m 10 --retry 3 "$PING_URL"The first line sets a variable for the lines below it. If the job fails, nothing is sent. The monitor goes down when the deadline passes.
Recommended: report start, duration and exit code#
Save this wrapper script. It pings /start, runs your command, then sends the command's exit code.
#!/bin/sh
# Usage: with-heartbeat.sh <command> [args...]
# Needs PING_URL set to your ping URL.
curl -fsS -m 10 --retry 3 "$PING_URL/start" > /dev/null
"$@"
status=$?
curl -fsS -m 10 --retry 3 "$PING_URL/$status" > /dev/null
exit $statuschmod +x /usr/local/bin/with-heartbeat.shPING_URL=https://<your-ping-url>
15 2 * * * /usr/local/bin/with-heartbeat.sh /usr/local/bin/backup.shSet the monitor's cron expression to the same 15 2 * * * and its timezone to the server's zone. Each job has its own ping URL. For a second job, set PING_URL again on the line before it.
The crontab tab under setup snippets shows the same pattern as one crontab line, with your URL filled in.