User Tools

Site Tools


programming:python:cwrap

This is an old revision of the document!


cron-wrap

What is it?

“Obviously, you're not a golfer”. But you are probably a sysadmin.

The cwrap.py script is meant to be used as a wrapper for cron jobs. The idea here is that you use this script to wrap whatever would be a normal cronjob for you to perform functions based on what options are specified. I think this is best explained with an example.

I have a cronjob that runs every minute. First, I don't want “overlap”, a second instance is run while the first one is still running, if a single run takes a long time. Solved. The nature of my cronjob (just a script that is being run) is such that there will be an occasional failure, but an occasional failure, in this case, can be ignored. Instead, I only want to know when the script has failed 5 times in a row. Solved. By default, cwrap.py will also swallow all the output for the occasional fails, but I at least want a record of those failures so that I can determine whether the intermittent fails are happening more frequently and constitute a different problem. Solved (by turning on the syslog option).

This script may not be useful for everyone, but for some out there, it will save you a lot of time, effort and probably emails in your inbox.

Getting a Copy of it

If cron-wrap sounds like something you might be interested in, head over to the github page to download it, or clone the repository.

Failures

The concept of a failure is simple. The exit code of the script that cwrap.py is running must be non-zero for cwrap.py to call it a “failure”.

Options

There are a plethora of options available here. See cwrap.py -h or man cwrap.py for the full lowdown.

Example

Here is a short example of how you would run a cron job with cwrap.py.

For the purpose of this example, we'll say that the normal cronjob is run as:

cron.sh -a do_stuff

Let's say that we always want to see the first failure in a string of failures (-f). Beyond the first failure, we don't want to be notified until the 10th consecutive failure (-n 10). This script can take a while to run so I want any other runs to retry two times (-r 2), but if it wasn't able to run because another instance was running, I don't want to hear about it (-i). I also don't want to see any normal output from it, but just when it fails (-q). Last, I want all errors to be logged to syslog (-S) under LOCAL1 (-C LOG_LOCAL1). Here would be the example:

cwrap.py -fSiq -n 10 -r 2 -C LOG_LOCAL1 cron.sh -a do_stuff
programming/python/cwrap.1329753373.txt.gz · Last modified: 2012/02/20 15:56 by jay