Monitoring Linux Cron Jobs

By: August Flanagan|Last Updated: Dec 07, 2022

After you’ve read about the multiple ways your cron jobs can fail, you’re probably wondering about the best way to keep an eye on them going forward.

Let’s take a look at a few helpful options you have to best monitor your cron jobs.

System Logs

Cron does have some system-level logging automatically in place.

On Debian and Ubuntu systems, in /var/log/syslog, the cron daemon reports when it gets started or restarted. On Redhat-based systems, the cron daemon writes log entries to /var/log/cron with the same kind of information.

The daemon also reports when it ran your jobs with log entries like this:

Mar 29 16:00:01 mywebserver CRON[12624]: (user) CMD (/home/user/bin/check-visitors.sh)

Unfortunately, cron does not report on the successes or failures of your scripts. It can’t even tell you if a job didn’t run in the first place.

Custom Metrics

You could write all of the measurement logic into the script that is running from the cron entry directly. That would entail logging events to your own log files or capturing the exit status of your script somewhere separately to be monitored by another process. You could also write any error messages that go to steddr to another error-logging file. This does add extra complexity to your script, though, because you would need to account for as many failure cases as possible.

External Monitoring

Thankfully, there is an easier option. Yes, you could go through all the trouble of making sure each of your scripts can notify you when something goes wrong, or you could capture all script output to special log files.

However, you could also take a look at Cronitor. Cronitor is a monitoring tool for your cron jobs, worker processes, and other services.

You can use their CronitorCLI software to automatically register all your cron jobs and make them visible and configurable from a single monitoring location on their website.

Here’s how you can get started.

2. Grab Your API Keys

Before you install the CronitorCLI software, you need to find your API key. This key is what uniquely identifies your account and tells CronitorCLI which dashboard to send results and metrics to.

Navigate to the settings page, and copy the API key that has the option CONFIGURE MONITORS set to Yes.

Settings page to API keys

API keys

Now you’re ready to install the CronitorCLI on your laptop or server.

3. Install the CronitorCLI

It’s recommended that you install CronitorCLI into the system-wide binary directory in /usr/bin. This will allow you to run the cronitor commands without specifying the full path. You can install the binary in another location, but you would need to specify the full path every time you run the command.

The default configuration file for CronitorCLI is stored in /etc/cronitor, so you will need sudo access to configure your instance of CronitorCLI.

Copy the commands below to install CronitorCLI to your device:

$ curl -sOL https://cronitor.io/dl/linux_amd64.tar.gz
$ sudo tar xvf linux_amd64.tar.gz -C /usr/bin/
$ sudo cronitor configure --api-key <your_api_key>

If the installation instructions above do not work, it’s possible that the installation procedure has changed since this article was written. If that’s the case, consult Cronitor’s website for CronitorCLI installation instructions.

The commands above accomplish the following:

  • First, the curl command downloads the latest binary (compressed using gzip) from Cronitor’s website.
  • The tar command (with sudo privileges) extracts the binary from the gzip file and copies it into /usr/bin.
  • The configure --api-key option tells the cronitor command which dashboard it should send metrics to.

Be sure to replace <your_api_key> with the key you copied from your Cronitor API keys page earlier.

You should see an output that looks something like this:

Configuration File:
/etc/cronitor/cronitor.json

Version:
30.2

API Key:
<your_api_key>

Ping API Key:
Not Set

Environment:
Not Set

Hostname:
linuxguy.invalid

Timezone Location:
{Africa/Johannesburg}

Your output will of course be distinct from the example above because your API key will be different, and probably so will your location and hostname.

4. Identify Jobs to Monitor with cronitor

The easiest way to tell cronitor which cron jobs to monitor is to run the following command:

$ sudo cronitor discover

This command will run through all the available crontab entries and interactively ask you whether you want to add them to your Cronitor dashboard for monitoring.

When it finds an entry, it will ask you to supply a job name for easy identification of the cron job on the Cronitor dashboard. If you don’t want to add a specific entry, just press Ctrl+C on that specific entry, and cronitor will continue scanning the rest of the available crontab entries for possible inclusion on your dashboard.

In the example below, there is a cron entry that pulls random facts about cats from an API and outputs it to a file for daily perusal. The output from cronitor discover should look like this. (Note that when the command discovers your cron job, it will ask you to name it so that it can be identified on your Cronitor dashboard. You can name it something short like “Cat Facts” for easy identification.)

$ cronitor discover
----► Scanning for cron jobs... (Use Ctrl-C to skip)
----► Checking user "linuxguy" crontab
 |--► Found 1 cron job:

	0 0 * * *  /home/linuxguy/scripts/random_catfacts.sh
	Job name: Cat Facts

 |--► Sending to Cronitor ✔
 |--► Integration complete ✔

----► Checking /etc/crontab
 |--► This crontab is not writeable. Re-run command with sudo. Skipping

----► Directory /etc/cron.d is not writable. Re-run command with sudo. Skipping
----► Discover complete ✔

Also note that in the example above, cronitor was not run with the sudo command. If you only want to monitor your user’s crontab, you don’t need to run it with sudo. If you want to monitor system-level crontab entries, you will need to run cronitor discover with sudo.

5. Monitor Your Cron Jobs with Cronitor.io

Back on the cronitor.io website, you should be greeted by the following message:

Congratulations, you’ve created your first monitor!

If you make any changes to your crontab entry—e.g., changing the schedule—you can just run cronitor discover again to update the statistics on cronitor.io.

Once a job has been successfully (or unsuccessfully) executed, you can see the status of your cron job on Cronitor:

Cat facts job status

If you click the Edit button for the specific job, you can set a few variables and thresholds:

Job edit

In the example above, the max duration has been set to thirty seconds. This is a simple script that pulls some information from a website, so if it’s running for longer than thirty seconds, you’d want to know about it.

You can also edit the “Alert Settings” for your cron job:

Alert Settings

This isn’t a mission-critical job (random cat facts, remember?), so as pictured, you only care if you haven’t received a new cat fact after five missed executions.

If, however, the script has failed after two executions, it means something is wrong—possibly because something beyond your control has changed. Maybe the operator of the website now expects you to pay a subscription fee to continue receiving your beloved cat facts, or the operator closed down their website completely because they now prefer dogs.

In any case, Cronitor can be used to monitor your cron jobs for any problem that might occur and give you insight into those problems so that you can investigate and fix issues wherever necessary.

Conclusion

In this article, you’ve learned the following:

  • What a cron job is
  • How the cron daemon works
  • How to create your own cron entry
  • What can go wrong with a cron job (hint: a lot of things!)
  • How to install CronitorCLI
  • How to use CronitorCLI to synchronize your crontab entries with Cronitor.io

Cronitor offers so much more than just cron job monitoring, and it does so at an affordable price.

Take a look at their offerings as well as their thorough documentation. They also offer SDKs for many popular programming languages, including but not limited to Python, Ruby, and Javascript. Finally, they offer an excellent API that you can use to control your Cronitor monitoring console from just about anywhere, without even touching the application frontend.

Cronitor can be an excellent utility for monitoring your applications, your API endpoints, and of course, your cron jobs.