← All guides

Guide · Linux & macOS

Monitor a shell script or cron job

If a script can run one line of curl, Mortemain can watch it. Works the same in a crontab line, a backup script, or a systemd unit.

1. Create a check

In your dashboard, create a check for this job and copy its unique ping URL. It looks like this (the UUID is the only secret, keep it unguessable):

 your ping URL
https://ping.mortemain.com/your-check-uuid

2. Ping when the job finishes

Append a curl to the end of your script. The flags matter: fail quietly, follow nothing, and time out so the ping can never hang your job.

 backup.sh
#!/usr/bin/env bash
pg_dump mydb | gzip > /backups/db.sql.gz

# tell Mortemain the job finished OK
curl -fsS -m 10 --retry 3 https://ping.mortemain.com/your-check-uuid

Prefer a one-liner? Chain it so the ping only fires if the job succeeded:

 crontab
# run at 03:00 and ping only on success
0 3 * * *  /usr/local/bin/backup.sh && curl -fsS -m 10 https://ping.mortemain.com/your-check-uuid

3. Alert on failure too (recommended)

Add a trap so a crash pings the /fail endpoint and forces the check down immediately, instead of waiting for the window to lapse.

 backup.sh
#!/usr/bin/env bash
set -euo pipefail
URL=https://ping.mortemain.com/your-check-uuid

# on any error, tell Mortemain the job failed
trap 'curl -fsS -m 10 "$URL/fail"' ERR

pg_dump mydb | gzip > /backups/db.sql.gz
curl -fsS -m 10 "$URL"          # success

4. Measure how long it took (optional)

Ping /start first and Mortemain will track each run's duration, so a backup that suddenly takes 4× longer becomes a warning, not a surprise.

 backup.sh
curl -fsS -m 10 "$URL/start"
pg_dump mydb | gzip > /backups/db.sql.gz
curl -fsS -m 10 "$URL"

Test it

Run the curl once by hand. The check should flip to up within a second or two. Then wait past its window without pinging, and you should get the down alert. That round-trip is the whole point: silence becomes the alarm.