How do you deploy cron jobs to production?

Using Fabric, I prefer to keep a pristine version of my crontab locally, that way I know exactly what is on production and can easily edit entries in addition to adding them.

The fabric script I use looks something like this (some code redacted e.g. taking care of backups):

def deploy_crontab():
    put('crontab', '/tmp/crontab')
    sudo('crontab < /tmp/crontab')

You can probably use something like CFEngine/Chef for deployment (it can deploy everything - including cron jobs)

However, if you ask this question - it could be that you have many production servers each running large number of scheduled jobs. If this is the case, you probably want a tool that can not only deploy jobs, but also track success failure, allow you to easily look at logs from the last run, run statistics, allow you to easily change the schedule for many jobs and servers at once (due to planned maintenance...) etc.

I use a commercial tool called "UC4". I don't really recommend it, so I hope you can find a better program that can solve the same problem. I'm just saying that administration of jobs doesn't end when you deploy them.


If you are using Fabric for deploment you could add a function that edits your crontab.

def add_cronjob():
    run('crontab -l > /tmp/crondump')             
    run('echo "@daily /path/to/dostuff.sh 2> /dev/null" >> /tmp/crondump')
    run('crontab /tmp/crondump')

This would append a job to your crontab (disclaimer: totally untested and not very idempotent).

  1. Save the crontab to a tempfile.

  2. Append a line to the tmpfile.

  3. Write the crontab back.

This is propably not exactly what you want to do but along those lines you could think about checking the crontab into git and overwrite it on the server with every deploy. (if there's a dedicated user for your project.)


You can also take a look at:

http://django-fab-deploy.readthedocs.org/en/0.7.5/_modules/fab_deploy/crontab.html#crontab_update

django-fab-deploy module has a number of convenient scripts including crontab_set and crontab_update