Chronic and Cron

I had Cron running my recollindex job at the desired time and I had sSmtp set up to email using POP through my gmail email account. The trouble was recollindex gives a lot of output and I was being sent an email with over 22,000 lines in it each time Cron ran the job!

I checked to see if there were command line options I could pass to recollindex to make it run silently or at least more quietly. No joy – recollindex doesn’t support that type of functionality.

I could make it silent by adding a redirect to null on the crontab command line like:

15 01 * * * recollindex >/dev/null 2>&1

But that would mean there would never be any output and there’d never be an email – whether recollindex ran successfully or otherwise.

I Googled a bit and found Chronic. This is a wrapper for Cron jobs. Chronic manages the output from other programs and arranges to only display that output if the command has an error or fails. If the command succeeds, any other output will be  hidden.  And that means no email. So you only get an email if it doesn’t work. Perfect.

To install Chronic type the following into a terminal:

sudo apt-get install moreutils

And to use it in the crontab listing, precede the command you wish to run with chronic, like this:

15 01 * * * chronic recollindex

And to test? Set the time just a few minutes into the future and let it run. You can use top to watch the process list and see that the processes are launched. If it all works according to plan you shouldn’t get an email.

I then changed the command line again so that it would generate an error. I added an extra x to the recollindex command. Because it was spelled incorrectly Cron wouldn’t be able to find a program by that name and that would generate an error. And because there had been an error I should get an email. So I used crontab and changed the line to say:

crontab -e
15 01 * * * chronic recollindexx

Note the extra x. I set the time to a few minutes in the future, let the job run and then checked my email. I had an email with the following subject:

Cron <dave@Nostromo> chronic recollindexx (failed)

and the following content:

Command ‘recollindexx’ not found in /usr/bin, /bin at /usr/bin/chronic line 44

Finally – success! Cron warns me by email if recollindex fails, and only if it fails. (The last step was to go back into crontab and remove the extra x and reset the time to my desired time of 01:15.)

Tags : , ,

6 thoughts on “Chronic and Cron”

    1. On the face of it, that sounds a neat way of doing it. I haven’t tried it, but the very fact that Chronic exists to solve this problem suggests that there might be more to that than meets the eye. If I get a moment I might give it a whirl and see what happens.

      1. After posting I did some more research and I found two possible enhancements of using Cronic over ‘> /dev/null’:

        + Cronic reports not only STDERR but STDOUT also and traces the error.

        + Cronic reports on exit status different from 0, regardless of whether an error message is output or not.

        BTW, I found “Chronic” spelled as “Cronic”. I guess we are talking about the same program…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.