Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 461
Chris Avatar asked Oct 21 '09 19:10

Chris


People also ask

Where do cron jobs deploy?

In order to configure your deployment cron job, you must use the correct repository path. To locate the desired repository's directory, navigate to cPanel's Git Version Control interface (cPanel >> Home >> Files >> Git Version Control).

How do I run a cron job automatically?

Cron is a job scheduling utility present in Unix like systems. The crond daemon enables cron functionality and runs in background. The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.


2 Answers

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

like image 51
kioopi Avatar answered Oct 06 '22 14:10

kioopi


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')
like image 10
Jesse Shieh Avatar answered Oct 06 '22 15:10

Jesse Shieh