Back to Monitoring guides

How to Monitor if Your Cron Jobs Run Correctly

Jenda Tovarys
Updated on November 23, 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

They 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
Jenda Tovarys
Jenda leads Growth at Better Stack. For the past 5 years, Jenda has been writing about exciting learnings from working with hundreds of developers across the world. When he's not spreading the word about the amazing software built at Better Stack, he enjoys traveling, hiking, reading, and playing tennis.
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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github