Guides
How to Monitor if Your Cron Jobs Run Correctly

How to Monitor if Your Cron Jobs Run Correctly

Author's avatar
Jan Tovarys
Updated on August 17, 2023

If you’re relying on cron jobs with your app, making sure they run correctly every single time is a must.

No one wants their scheduled database backup or an email campaign to fail and not know about it.

In this post, you’ll learn how to set up a system that automatically monitors your cron jobs and alerts you in case of any issues.

How to set up cron monitoring

You need a monitoring tool like Better Stack to do this, which is now used by over 150.000 developers.

⚖️ What’s the difference: Cron job vs. heartbeat monitoring

There are, in most cases the same. Cron job monitoring specifically refers to monitoring the specific job scheduler: cron. Heartbeat monitoring is a general name for monitoring any software system or device by expecting a regular “heartbeat” (usually a GET request) on a dedicated endpoint.

Free method (up to 10 monitors)

Sign up for Better Stack, go to the Heartbeats tab, and create a new heartbeat. There you can create up to 10 monitors for free.

Screenshot 2023-08-16 at 11.40.00.png

You can specify how often your cron runs. If it’s an hourly database, make it every hour. The grace period is there to prevent false positives - in case something runs a little slower.

Screenshot 2023-08-16 at 11.48.36.png

Then pick how you want to get alerted if something goes wrong.

Screenshot 2023-08-16 at 11.44.22.png

Now copy the unique monitor URL and add a simple HEAD, GET, or POST request to it at the end of the cron job script.

Screenshot 2023-08-16 at 11.50.32.png

Here is an example of how this would look like in a 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"

    /usr/local/bin/aws s3 cp "$file" s3://uptime-dbbackups/

    rm "$file"

    # you get this URL in the Uptime dashboard
    curl "https://uptime.betterstack.com/api/v1/heartbeat/XXXXXXXXXXXXXXXXXXXXXX"

What happens here is that the Heartbeat URL we've created above expects a GET or POST request every day since having made the first request.

If the code above fails, our background job won't make the request to the Heartbeat URL. In that case, the heartbeat will alert the current on-call person and create an Incident.

To have more than 10 monitors, Sign up for Better Stack and choose any of the paid plans. Paid plan also includes other features, including regular uptime monitoring, incident alerting with on-call and status pages.

How to run cron jobs everywhere and anytime

Here are quick tutorials how to set up cron jobs in different environments:

And here are the most common cron timing setups and how to do them:

Author's avatar
Article by
Jan Tovarys
Check Uptime, Ping, Ports, SSL and more.
Get Slack, SMS and phone incident alerts.
Easy on-call duty scheduling.
Create free status page on your domain.
Got an article suggestion? Let us know
Next article
What is API Monitoring?
Learn what is API monitoring, how does it work, what are the benefits and drawbacks and how to set it up.
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.