Get alerted when a cron job or app stops running
A heartbeat check watches something that should happen on a schedule, like a nightly import or a queue worker. Your job pings a private address each time it runs. If a ping doesn't arrive in time, or the job reports a failure, we alert you. Heartbeat checks come with Enhanced Monitoring.
An uptime check tells you when your site stops answering. A heartbeat tells you when work behind the scenes stops happening, which is often the harder problem to notice.
Before you start
- Your account needs Enhanced Monitoring. You can add up to 10 heartbeat checks.
- Know roughly how often the job runs, for example every hour or once a day.
Add a heartbeat check
- Open Account → Hosting → your site → Monitoring.
- Next to Heartbeats, click Set up, or Manage if you already have one.
- Give it a name you'll recognize in an alert, like
Nightly product import. - Set Expected every to how often the job runs.
- Set Grace time to how late a ping can be before we alert you. A job that sometimes takes 20 minutes needs at least 30 minutes of grace.
- Click Add heartbeat, then Copy to copy its address. It looks like this:
https://barbrick.ca/hook/monitor/hb/Xk3v9QpL2mN8rT5wY7zA1bC4dE6fG0hJ
Important: Treat the address like a password. Anyone who has it can report runs for this check. If it leaks, click New address and update your job. The old address stops working at once.
Connect a cron job
Cron jobs in the control panel ping the heartbeat for you.
- Open Cron jobs and click Edit on the job.
- Make sure Keep a record of each run is on.
- Paste the address into Heartbeat address.
- Click Save job.
After each run, a run that worked pings the address. A run that ends with an error pings the address with /fail on the end, so you hear about the failure straight away instead of waiting for the grace time to pass.
Ping from your own code
Any script, app or service that can make a web request can ping a heartbeat. Add one request at the end of the work.
| Address | Means |
|---|---|
…/hb/<code> |
The run worked |
…/hb/<code>/fail |
The run failed. We alert you now |
…/hb/<code>/start |
A run started. Optional, and it never triggers an alert |
Plain GET and POST requests both work, and we ignore anything you send in the body.
#!/bin/sh
# Run the import, then report how it went.
php /home/example/import.php
if [ $? -eq 0 ]; then
curl -fsS -m 10 https://barbrick.ca/hook/monitor/hb/Xk3v9QpL2mN8rT5wY7zA1bC4dE6fG0hJ
else
curl -fsS -m 10 https://barbrick.ca/hook/monitor/hb/Xk3v9QpL2mN8rT5wY7zA1bC4dE6fG0hJ/fail
fi
<?php
// At the end of a PHP job. A failed ping never stops the job.
$ok = run_import();
$hb = 'https://barbrick.ca/hook/monitor/hb/Xk3v9QpL2mN8rT5wY7zA1bC4dE6fG0hJ';
@file_get_contents($ok ? $hb : $hb . '/fail', false, stream_context_create(['http' => ['timeout' => 10]]));
Tip: Keep the timeout short, as in the examples, so a slow network never holds up your job.
What happens when a ping is late
We compare each heartbeat with its schedule every few minutes. When no ping has arrived within Expected every plus Grace time, the check turns red, an incident opens and we alert your contacts. The next successful ping closes the incident and sends a recovery alert.
| Example | Expected every | Grace time | Alert if no ping by |
|---|---|---|---|
| Hourly sync, last ping at 2:00 PM | 1 hour | 10 minutes | 3:10 PM |
| Nightly backup, last ping at 1:05 AM | 1 day | 1 hour | 2:05 AM the next day |
Alerts go to the same contacts as your uptime alerts, and maintenance windows don't apply to heartbeats. To stop alerts while you work on a job, click Pause on the heartbeat. A paused heartbeat ignores pings and never alerts. When you resume it, the schedule starts again from that moment, so you won't get an alert for runs missed while it was paused.
If something goes wrong
| Problem | Fix |
|---|---|
| You get a missed-heartbeat alert, but the job ran | The run took longer than the grace time, or the ping request failed. Raise Grace time, and check that the job can make outgoing web requests |
| The ping answers "not found" | The address was replaced with New address, the heartbeat is paused, or Enhanced Monitoring has ended. Copy the current address from Monitoring |
| A cron job never pings | Check that Keep a record of each run is on and the address starts with https:// |
| Too many pings are refused | Each heartbeat accepts up to 12 pings a minute. Ping once per run, not in a loop |
Still stuck? Ask us and we'll walk you through it.