⏰ 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
| العلم | المعنى |
|---|---|
-f | Fail silently on HTTP errors |
-s | Silent mode (no progress bar) |
-S | Show errors even in silent mode |
--retry 3 | Retry 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
)