Cron
Create a heartbeat check with the same schedule as the job, then add one curl to the end of its crontab line. Nothing to install: curl is already on the box.
The block
Paste this onto the end of the job's line in crontab -e, and change only the ping
UUID, which is on the check's page under Ping URL:
&& curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID
So a nightly backup at 03:00 becomes:
0 3 * * * /opt/backup.sh && curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID
The check turns green on the job's next run. Keep it on one line: cron does not continue a line ending in a backslash.
Why every part is there
Each of these looks like noise and is not. Take one out and the check still turns green, until the night it tells you something that did not happen.
&& | Pings only when the job exits 0. A job that
failed sends nothing, so the check goes red when its ping is late instead of green on a broken
run. Replacing it with ; is the one edit that makes a failing job report
success. |
-f | Makes curl fail on a 4xx or 5xx. Without it, curl exits
0 on an error page, so a mistyped UUID looks exactly like a ping that
landed. |
-s | Hides curl's progress meter, which cron would otherwise mail you after every run. |
-S | Still prints a real error with -s on, so a ping that
failed reaches cron's mail instead of vanishing. |
-m 10 | Gives each attempt at most 10 seconds, so a slow answer can never leave the line hanging. |
--retry 3 | Retries a timeout or a transient error three times, waiting as long as Marsolen asks, so a flapping network does not report a job that ran fine as late. |
-o /dev/null | Throws away the OK the ping answers with,
which cron would otherwise mail you after every successful run. |
Things cron does to you
Cron's PATH is minimal
Cron runs jobs with a short PATH, often only /usr/bin:/bin, not the one
your shell has. Run command -v curl: if it prints anything outside those two, write
the full path into the line (/usr/local/bin/curl), or put a PATH= line
at the top of the crontab. A curl cron cannot find fails loudly rather than quietly: the line
exits with an error, cron mails it, and the check goes red when the ping is late.
% means something to cron
In a crontab line, an unescaped % ends the command and the rest becomes its input, so
date +%F silently runs a different command. Escape every one as \%:
0 3 * * * tar czf /backup/db-$(date +\%F).tgz /var/lib/db && curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID
The block itself contains no %, so it never needs escaping.
The schedule is in the box's time zone
Cron reads 0 3 * * * in the server's own time zone. Give the check the same one
(timedatectl shows it), or the check expects the job hours before or after it
runs. To see when an expression next runs in that zone, and what it does on the nights the clocks
change, paste it into the cron expression explainer.
Being told about a failure at once
With &&, a failed run is noticed when its ping is late: after the check's grace
time. To be alerted on the next sweep instead, with the exit code in the alert, send the code
itself: replace && with ; and end the URL with
/$?. A 0 is a success and anything else a failure.
0 3 * * * /opt/backup.sh; curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID/$?
Saying why it failed
A failure can carry the end of the job's output, so the alert says why and not only that. Tick Keep the output of failed runs on the check's form, then send the output as the request's body. We keep the last 4 kB, for failures only, as long as we keep the ping, and blank the obvious secrets in it; send only what you would paste into a ticket.
0 3 * * * /opt/backup.sh > /tmp/backup.log 2>&1; code=$?; tail -c 4000 /tmp/backup.log | curl -fsS -m 10 --retry 3 -o /dev/null --data-binary @- https://staging.marsolen.com/ping/YOUR-PING-UUID/$code
Send a tail, as above, rather than the whole log: a request over 64 kB is refused. Email alerts quote all of what is kept; chat channels, PagerDuty and webhooks get its last line.
Other ways in
On Spring Boot, the starter creates and pings the check from
the @Scheduled method itself. To create checks from a script or CI, see
the API.