Getting started with heartbeats?
Start with our Introduction to cron job monitoring or Getting started with cron jobs guide.
A heartbeat monitor helps you track periodic tasks by expecting regular requests to a unique URL.
If the expected heartbeat is not received within the specified frequency and grace period, it triggers an incident and alerts the configured on-call team.
Start with our Introduction to cron job monitoring or Getting started with cron jobs guide.
The heartbeat remains in a "Pending" state until the first request is received. The monitoring period begins from the first heartbeat received, and incidents are only raised after the "expected frequency" and "grace period" have elapsed.
In this example, we’re using the cron-job style syntax to schedule a periodic task. Cron allows you to define tasks based on time intervals such as minutes, hours, days, etc.
To monitor a daily database backup, set up a CRON task to execute your job every day at midnight:
0 0 * * * ruby /home/deploy/backup_database.sh >/dev/null 2>&1
The CRON expression 0 0 * * *
means the job will run at midnight every day.
Then, include a curl
request to the heartbeat URL in your backup script:
#!/usr/bin/env bash
set -o errexit
set -o xtrace
date=`date "+%Y-%m-%d_%H:%M:%S"`
file="/dumps/uptime.betterstack.$date.dump"
time dokku postgres:export uptime > "$file"
aws s3 cp "$file" s3://uptime-dbbackups/
rm "$file"
# Notify the heartbeat URL
curl "https://uptime.betterstack.com/api/v1/heartbeat/<HEARTBEAT_TOKEN>"
You can explicitly report a failure by appending /fail
to the heartbeat URL:
curl "https://uptime.betterstack.com/api/v1/heartbeat/<HEARTBEAT_TOKEN>/fail"
Additionally, you can include an exit code or output when reporting a failure. For example:
backup_output=$(./run-backups.sh 2>&1)
curl -d "$backup_output" \
"https://uptime.betterstack.com/api/v1/heartbeat/<HEARTBEAT_TOKEN>/$?"
This sends both the exit code and the script output to help diagnose the failure.