Skip to content

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:

OptionWhat it does
-fTreat an HTTP error status as a failure.
-sSHide progress output, but still show errors.
-m 10Give up after 10 seconds.
--retry 3Retry 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.

crontab
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.

Save this wrapper script. It pings /start, runs your command, then sends the command's exit code.

/usr/local/bin/with-heartbeat.sh
bash
#!/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 $status
Make it executable
chmod +x /usr/local/bin/with-heartbeat.sh
crontab
PING_URL=https://<your-ping-url>
15 2 * * * /usr/local/bin/with-heartbeat.sh /usr/local/bin/backup.sh

Set 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.

Last updated