How do you deploy cron jobs to production?

后端 未结 9 1004
北海茫月
北海茫月 2021-02-01 18:42

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

相关标签:
9条回答
  • 2021-02-01 19:26

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

    0 讨论(0)
  • 2021-02-01 19:27

    If you're using Django, take a look at the jobs system from django-command-extensions.

    The benefits are that you can keep your jobs inside your project structure, with version control, write everything in Python and configure crontab only once.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题