Volunteer Recruiting

Volunteer recruiting tricks, tips, and techniques

Volunteer Management

Tips on managing volunteers and volunteer-based projects.

Staff/Volunteer Leadership

Improve your leadership of volunteers for your cause

Field Notes

Guest posts and ideas from leaders in the trenches

In The News

The latest news articles related to volunteering

Home » Technology

Cron Wrapper Ruby Script

Submitted by James Higginbotham on March 9, 2006 – 11:06 pmNo Comment

A few weeks ago, I had need of a simple way to wrap the output of a typical cron script into an easy-to-read output. For those not familiar, cron allows a user to run a script at a specific time either once or reoccuring. For these scripts they were nightly jobs such as backing up my databases and archives changed files to another system. Cron can be configured to email the result of the script, whether it succeeds or fails, to a specific email address. The downside to this is that it doesn’t tell you the result, so you have to scan the entire output to find out if it failed, often scrolling to the end looking for an error message. I really don’t care to see the output if it succeeds, but should it fails, I want to know what happened. Here is what I did:

  1. Write a wrapper script that cron will invoke, passing it the command line I really want to run as the rest of the arguments
  2. Have the wrapper script run the actual command, capturing any standard output (STDOUT) or error output (STDERR)
  3. If the actual command succeeds, print a success message, then the details of the run; if it failed, print a failed message and show the error output

Being a guy that writes Java during the day and aspires to write Ruby during the day, I decided to brush up a little on using Ruby to write scripts. The final script is called cronwrapper (original, huh?) and is available if you would like to use it or just want to see what I did: cronwrapper.rb

As a side effect of what I did, GMail’s feature to show the first few words of an email now shows me if it succeeded or failed, without me even having to open the email: Gmail view

Future improvements or changes you may want to make include: adding a syslog line to allow tools like Swatch to catch an error message and notify someone, suppress output on success, or send the email outside of cron with the output as an attachment rather than a plain text message.

BTW, I tested the script on Windows and Gentoo Linux and it works great on both platforms using Ruby 1.8.3.

Feedback and improvements welcome. Enjoy!

Popularity: 3% [?]

Comments are closed.