All Articles/Integrations & CI/CD

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

  1. Go to Monitors in the dashboard.
  2. Click Create Monitor and select Heartbeat type.
  3. Set the expected interval (e.g., every 1 hour).
  4. 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-token

Pinging 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:

  1. Captures the exception as an error log
  2. Does not send the ping
  3. 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.