Sorry this page looks weird. It was automatically migrated from my old blog, which had a different layout and different CSS.

FYI: Find Out What Cron Is Doing

How Do You Know If Cron Worked?

Most people who deploy apps set up cron tasks on their servers. Typically these rotate search engine indexes, generate reports, etc. But how do you know if your cron tasks are still working? As time goes by processes get restarted; perhaps they come back up with a different owner, with different permissions. Or maybe you upgrade a gem and a months-old maintenance task you’d forgotten about breaks.

For a long time I got around this by redirecting standard error to standard out and writing all of it to a log file, like this:

0 0,3,6,9,12,15,18,21 * * *
  cd /var/www/apps/someapp/current && /opt/ree/bin/rake RAILS_ENV=production thinking_sphinx:index
  >> /var/www/apps/someapp/current/log/cron.log 2>&1

But I could never quite remember the pipe redirection syntax, and once you have a few of these cluttering your crontab it’s really hard to see what tasks you’re actually running. Furthermore you have to remember to check your log files on a regular basis. We all have better things to do.

Cron is designed for scheduling tasks. It is not designed for keeping you posted on how they worked out.

FYI Keeps You Posted

Here’s the task (from above) I want to schedule:

0 0,3,6,9,12,15,18,21 * * *
  cd /var/www/apps/someapp/current && /opt/ree/bin/rake RAILS_ENV=production thinking_sphinx:index

When this runs, I want standard out and standard error logged somewhere, and I want to be emailed if it fails. And I want all this to happen with as little change as possible to my crontab. Here’s what I came up with:

0 0,3,6,9,12,15,18,21 * * *
  fyi "cd /var/www/apps/someapp/current && /opt/ree/bin/rake RAILS_ENV=production thinking_sphinx:index"

That’s 6 extra characters. Pretty minimal. And it’s memorable. Minimal and memorable!

By default this will log everything to fyi.log in your home directory. If you supply some email connection details in a configuration file, you’ll get emailed if it fails.

Installation

Install my fyi gem and you’re ready to go:

$ sudo gem install fyi

You can change its log file location via a simple YAML config file. See the example configuration file.

Email Notification

Switch this on by supplying some SMTP details in the config file. By default it’ll email you when your tasks fail, but you can also have it email you when they succeed. This is useful when you are setting up a new task in cron and want to check for a few days that it’s working properly.

Campfire / HTTP / etc Notification

fyi is trivial to extend with additional notifiers. Just write a subclass of Fyi::Notifier which responds to :notify and pop any configuration you need in the config file. The README has more information.

Statistical Proof of Marvellousness

The task I want to schedule is 124 characters long. The pipe-redirection-plus-remembering-to-check method is 175 characters long, always needs Googling, and doesn’t really work. The fyi method, which is marvellous, is a mere 130 characters. That’s 88% less crud in your crontab!

Comments

Fantastic, I think I’ll be using this! :)

Olly • 10 August 2009

Cron will email you if you configure it correctly.

I guess you don’t get dailies, weeklies and monthlies for your servers either, which I can’t recommend.

raggi • 14 August 2009

Andrew Stewart • 10 August 2009 • Deployment
You can reach me by email or on Twitter.