Skip to main content

โฐ Cron Job Monitoring

The most common use for heartbeats is monitoring cron jobs. Here's how to add monitoring to your crons.


๐Ÿง Linux Cronโ€‹

Basic Patternโ€‹

# Your existing cron
0 2 * * * /path/to/backup.sh

# With Monitron heartbeat
0 2 * * * /path/to/backup.sh && curl -fsS --retry 3 https://your-monitron.com/heartbeat/TOKEN

With Failure Reportingโ€‹

0 2 * * * /path/to/backup.sh && curl -fsS https://your-monitron.com/heartbeat/TOKEN || curl -fsS -X POST https://your-monitron.com/heartbeat/TOKEN/fail

This sends a success ping if the script exits 0, or a failure report if it exits non-zero.

curl Flags Explainedโ€‹

FlagMeaning
-fFail silently on HTTP errors
-sSilent mode (no progress bar)
-SShow errors even in silent mode
--retry 3Retry up to 3 times on failure

๐ŸชŸ Windows Task Schedulerโ€‹

Add this to the end of your scheduled task's script:

PowerShellโ€‹

# At the end of your script
try {
# Your job logic here
Do-Something

# Report success
Invoke-RestMethod -Uri "https://your-monitron.com/heartbeat/TOKEN" -Method GET
} catch {
# Report failure
Invoke-RestMethod -Uri "https://your-monitron.com/heartbeat/TOKEN/fail" -Method POST
throw # Re-throw so Task Scheduler sees the failure
}

Batch Fileโ€‹

your-script.exe
IF %ERRORLEVEL% EQU 0 (
curl -fsS https://your-monitron.com/heartbeat/TOKEN
) ELSE (
curl -fsS -X POST https://your-monitron.com/heartbeat/TOKEN/fail
)