Setting Up Heartbeat Monitors for Cron Jobs
Monitor cron jobs and background tasks with heartbeat pings.
What Is a Heartbeat Monitor?
A heartbeat monitor expects a periodic ping. If the ping doesn't arrive within the expected interval, the monitor fires an alert. Use it for cron jobs, scheduled tasks, and background processes.
Setup
- Go to Monitors in the dashboard.
- Click Create Monitor and select Heartbeat type.
- Set the expected interval (e.g., every 1 hour).
- Copy the generated token.
Pinging from a Cron Job
Terminal
# Crontab entry — pings after successful completion
0 * * * * /usr/local/bin/sync-inventory && curl -s -X POST $LOGNITOR_API_URL/heartbeat/your-tokenPinging from Your Application
JavaScript
import { heartbeat } from '@lognitor/node';
const hb = heartbeat('your-monitor-token');
// Simple ping
await hb.ping();
// Wrap a job — pings on success, captures exception on failure
await hb.wrap(async () => {
await syncInventory();
});Failure Handling
If the wrapped function throws an error, the heartbeat:
- Captures the exception as an error log
- Does not send the ping
- Re-throws the error
The missing ping triggers the monitor alert, and the captured exception tells you why.
Ping after, not before
Always ping after the job completes successfully. If you ping before, a failed job still looks healthy.