Search code examples
djangogitcron

How do you deploy cron jobs to production?


How do people deploy/version control cronjobs to production? I'm more curious about conventions/standards people use than any particular solution, but I happen to be using git for revision control, and the cronjob is running a python/django script.


Solution

  • 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.)