Monitor Website Health Endpoint Every 5 Minutes
Automated health checks catch downtime before your users do. This recipe polls a URL every five minutes, verifies the HTTP status code is in the 2xx range, and triggers an alert the moment something goes wrong. Pair it with a CronJobPro heartbeat monitor so you also know the checker itself is running reliably.
Schedule
*/5 * * * *Every 5 minutes, around the clock
Setup
- 1
Define the target URL and alert email
Set the TARGET_URL variable in your script to the health endpoint you want to monitor, for example https://yourapp.com/health. Set ALERT_EMAIL to the address that should receive failure notifications. If your endpoint requires an Authorization header, add it to the curl command with -H.
- 2
Place the script on your server and make it executable
Save the script to a path such as /usr/local/bin/check-health.sh and run chmod +x /usr/local/bin/check-health.sh. Ensure curl and mail (or sendmail) are installed. On Debian/Ubuntu: apt install curl mailutils.
- 3
Create a CronJobPro heartbeat monitor
In CronJobPro, create a new Heartbeat monitor with a period of 5 minutes and a grace period of 2 minutes. Copy the generated ping URL, which looks like https://cronjobpro.com/ping/<token>, and paste it into the HEARTBEAT_URL variable in your script.
- 4
Register the cron entry
Add the job to your crontab with crontab -e and paste the line: */5 * * * * /usr/local/bin/check-health.sh >> /var/log/health-check.log 2>&1. The log file lets you review historical output without relying solely on alerts.
- 5
Test the end-to-end flow
Run the script manually once to confirm it exits cleanly and that the heartbeat ping reaches CronJobPro (you will see the last-ping timestamp update in your dashboard). Then temporarily set TARGET_URL to a URL that returns a non-2xx status and verify you receive the failure alert email.
The script
bash
#!/usr/bin/env bash
set -euo pipefail
# --- Configuration ---
TARGET_URL="https://yourapp.com/health"
ALERT_EMAIL="you@example.com"
HEARTBEAT_URL="https://cronjobpro.com/ping/REPLACE_WITH_YOUR_TOKEN"
TIMEOUT_SECONDS=10
# --- Perform the health check ---
HTTP_STATUS=$(curl \
--silent \
--output /dev/null \
--write-out "%{http_code}" \
--max-time "$TIMEOUT_SECONDS" \
"$TARGET_URL" \
|| echo "000")
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [[ "$HTTP_STATUS" =~ ^2 ]]; then
echo "[$TIMESTAMP] OK - $TARGET_URL returned HTTP $HTTP_STATUS"
# --- Notify CronJobPro that this run succeeded ---
curl --silent --max-time 5 "$HEARTBEAT_URL" > /dev/null
exit 0
else
echo "[$TIMESTAMP] FAIL - $TARGET_URL returned HTTP $HTTP_STATUS"
# --- Send a plain-text alert email ---
echo "Health check failed for $TARGET_URL at $TIMESTAMP. HTTP status: $HTTP_STATUS." \
| mail -s "[DOWN] Health check failed: $TARGET_URL" "$ALERT_EMAIL"
# --- Tell CronJobPro the job failed so it can alert via its own channels ---
curl --silent --max-time 5 "${HEARTBEAT_URL}/fail" > /dev/null
exit 1
fiMonitor it
Create a Heartbeat monitor in CronJobPro with a period of 5 minutes and a grace period of 2 minutes. The script calls https://cronjobpro.com/ping/token on every successful run, resetting the countdown. If the ping does not arrive within the period plus the grace window, CronJobPro treats the job as missed and fires an alert through whichever channels you have configured: email, Slack, Discord, Microsoft Teams, PagerDuty, Opsgenie, or a custom webhook. On an explicit failure the script calls /ping/token/fail immediately instead of waiting for the timeout, so you get a faster alert when the endpoint is definitively down rather than merely silent. This two-signal design catches both the case where your server's cron daemon stops firing and the case where the job runs but the target endpoint is unhealthy.
Frequently asked questions
What HTTP status codes count as healthy?
The script treats any response whose first digit is 2 as healthy, covering 200 OK, 201 Created, 204 No Content, and all other 2xx codes. If your API returns a 301 or 302 redirect and you want to follow it, add the --location flag to the curl command; otherwise redirects will be treated as failures.
How do I monitor an endpoint that requires authentication?
Pass the credentials as a header with curl's -H flag, for example -H 'Authorization: Bearer YOUR_TOKEN', or use --user username:password for Basic authentication. Store secrets in a restricted file (chmod 600) and source it at the top of the script rather than hard-coding credentials.
Can CronJobPro run this check directly without a server-side script?
CronJobPro can trigger HTTP endpoints on a schedule, meaning it can call your health URL itself. For a simple status check you can point a CronJobPro HTTP job directly at the endpoint and let it evaluate the response code. The bash script approach is better when you need custom logic, authentication handling, or want to send your own alert emails alongside CronJobPro notifications.
What happens if the server running cron goes offline?
If the server goes down, cron stops firing and the heartbeat ping never reaches CronJobPro. Because the heartbeat has a period and grace window, CronJobPro will alert you after that window expires, even though no explicit failure was reported. This is the primary reason to use a heartbeat monitor in addition to any in-script alerting.