How to monitor cron jobs
Use Monibot to ensure your cron job executes regularly and successfully. If the cron job fails to execute successfully, Monibot will notify you.
A nightly database backup
For the purpose of this guide, let's say you have set up a shell script that backups a sqlite database. The script might look like this:
$ cat backup.sh
#!/bin/sh
set -e
cd $(dirname "$0")
sqlite3 app.db '.backup app.db.1'
Thanks to the set -e
line, the script is 'fail fast'.
That means it will exit immediately if one if its commands encounters an error.
Let's further assume you have set up a cron job that executes that script each night at 2:00 am:
$ crontab -l
# m h dom mon dow command
0 2 * * * /home/user/backup.sh
Every night at 02:00, the script will be executed automatically by cron. Now the question is: How can you know if the backup script succeeds or fails? Let's use Monibot for monitoring that cron job.
Create a Monibot watchdog
- Log into your Monibot account. If you don't have one, register now, it's free.
- In the main menu, click 'Watchdogs'. You'll see a 'New Watchdog' button.
- Click the 'New Watchdog' button. Enter a name for the watchdog, e.g. "backup.sh". You can chose any name here, it's used only for display.
- Enter a suitable heartbeat interval. The cron job should be fired every night, and the backup should be done in just a few seconds. Enter '25h' as interval, that means 25 hours.
- Click 'Save'. Monibot creates the watchdogs and assigns a unique id to it, which you will need later.
Your watchdog will now listen for heartbeats from your server. If there is no heartbeat for 25 hours, Monibot will notify you. Next, make sure that your cron job sends heartbeats to Monibot every time the backup script is executed.
Send heartbeats to Monibot
To send heartbeats, you can use the curl
command or our moni
command line tool,
whatever you like best. We will describe both options.
1. Send heartbeats with curl
In your backup script, append the curl invocation like this:
$ cat backup.sh
#!/bin/sh
set -e
cd $(dirname "$0")
sqlite3 app.db '.backup app.db.1'
# send heartbeat to Monibot
MONIBOT_API_KEY=0000000000
WATCHDOG_ID=8888888888
curl -X POST https://monibot.io/api/watchdog/$WATCHDOG_ID/heartbeat \
-H "Authorization: Bearer $MONIBOT_API_KEY"
Please replace MONIBOT_API_KEY
and WATCHDOG_ID
with your values.
You can see your API Key in your Monibot account profile.
2. Send heartbeats with moni
As an alternative to curl
, you can use our moni
command line tool
(see https://github.com/cvilsmeier/moni).
Using the moni
command line tool has the slight advantage that
in case of temporal network outages, it retries a few times before giving up.
In your backup script, append the moni
invocation like this:
$ cat backup.sh
#!/bin/sh
set -e
cd $(dirname "$0")
sqlite3 app.db '.backup app.db.1'
# send heartbeat to Monibot
MONIBOT_API_KEY=0000000000
WATCHDOG_ID=8888888888
~/bin/moni beat $WATCHDOG_ID
Please replace MONIBOT_API_KEY
and WATCHDOG_ID
with your values.
You can see your API Key in your Monibot account profile.
Summary
In this guide we've described how to easily setup a watchdog heartbeat for a sample cron job. If the cron job succeeds, nothing happens. However, if the cronjob fails to run successfully in the desired interval, Monibot will notify you.