Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Monitor Cron Jobs with a Simple HTTP Health Check

How to Monitor Cron Jobs with a Simple HTTP Health Check Cron jobs are great for automating repetitive tasks, but there is one problem: What happens when the job stops running? A cron entry can exist for months without anyone noticing that the script has started failing. For example, you might have a backup job running every night: 0 2 * * * /path/to/backup.sh ` Everything looks fine until the…

Cron jobs are beneficial for automating recurring tasks, but they present a challenge: if the job ceases functioning, it may go unnoticed for an extended period. For instance, a backup job scheduled daily could silently fail without anyone's knowledge until it's too late. One effective solution is to configure the job to send an HTTP request upon successful completion.

The basic concept involves modifying the job to not only run but also report its health status. For example, a cron job executing every day at 2 AM could run a backup script and then make an HTTP request to a monitoring endpoint if the task completed successfully. This check-in is recorded by a monitoring service. If the system does not receive this check-in within the set timeframe, an alert is triggered, thus providing a simple "dead man's switch" for the scheduled job.

One significant advantage of this approach is that it does not require any additional software or agents to be installed on the server. The existing script can make the HTTP request after a successful run. This method is applicable to a variety of scenarios, including Linux cron jobs, Windows scheduled tasks, database backups, import/export jobs, background scripts, and data processing jobs. Even platforms like n8n, Zapier can utilize this method for monitoring.

An example of implementing this would be in a shell script. The monitoring logic can be integrated within the script itself. For instance, a backup script could be executed, and if it returns a zero exit status (indicating success), an HTTP request to a monitoring endpoint is made. This way, the monitoring request is only sent upon successful completion, and if a check-in is missed, an alert can be generated.

This monitoring pattern, not limited to cron jobs, can also be utilized for other automated processes. For instance, a scheduled database export running every six hours could simply check in upon successful completion. The same principle applies to automation platforms like n8n, Make, and Zapier, where the process only needs to confirm its successful execution.

The author of this article, Doyouok, has developed a service to offer straightforward monitoring for processes that should run continuously. It employs HTTP check-ins and sends alerts when a check-in is missed, without necessitating any installation on the machine running the job. Free plans are available, allowing users to create monitoring checks for their cron jobs, scheduled tasks, scripts, backups, and automation workflows. You can visit Doyouok at https://doyouok.com/.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Why my builds don't run on my laptop

My React Native app has never been compiled on my own machine. Not once. That started as a limitation and turned into the thing that keeps my releases boring.

More from Sunday 20 September →