23👍
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).
-
Save the crontab to a tempfile.
-
Append a line to the tmpfile.
-
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.)
10👍
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')
- [Django]-Django: Multiple forms possible when using FormView?
- [Django]-Django: Passing HTML string to template
- [Django]-Django variable in base.html
4👍
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
- [Django]-Automatically import models on Django shell launch
- [Django]-Python Requests POST doing a GET?
- [Django]-Best practice for Python & Django constants
1👍
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.
- [Django]-PyCharm code inspection complains template file not found, how to fix?
- [Django]-Django Style: Long queries?
- [Django]-Django.db.utils.OperationalError Could not connect to server
1👍
There are really 3 options of manually deploying a crontab if you cannot connect your system up to a configuration management system like cfengine/puppet.
You could simply use crontab -u user -e but you run the risk of someone having an error in their copy/paste.
You could also copy the file into the cron directory but there is no syntax checking for the file and in linux you must run touch /var/spool/cron in order for crond to pickup the changes.
Note Everyone will forget the touch command at some point.
In my experience this method is my favorite manual way of deploying a crontab.
diff /var/spool/cron/<user> /var/tmp/<user>.new
crontab -u <user> /var/tmp/<user>.new
I think the method I mentioned above is the best because you don’t run the risk of copy/paste errors which helps you maintain consistency with your version controlled file. It performs syntax checking of the cron tasks inside of the file, and you won’t need to perform the touch command as you would if you were to simply copy the file.
- [Django]-@csrf_exempt does not work on generic view based class
- [Django]-How can I change the modelform label and give it a custom name
- [Django]-Foreign Key Django Model
1👍
Having your project under version control, including your crontab.txt, is what I prefer. Then, with Fabric, it is as simple as this:
@task
def crontab():
run('crontab deployment/crontab.txt')
This will install the contents of deployment/crontab.txt
to the crontab of the user you connect to the server. If you dont have your complete project on the server, you’d want to put
the crontab file first.
- [Django]-Django Standalone Script
- [Django]-How can I render a ManyToManyField as checkboxes?
- [Django]-How to call a static methods on a django model class during a south migration
0👍
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.
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
- [Django]-Django SMTPAuthenticationError
- [Django]-Iterate over model instance field names and values in template
0👍
I use Buildout to manage my Django projects. With Buildout, I use z3c.recipe.usercrontab
to install cron jobs in deploy or update.
- [Django]-Template filter to trim any leading or trailing whitespace
- [Django]-Django setting environment variables in unittest tests
- [Django]-Multithreading for Python Django
0👍
You said:
I’m more curious about conventions/standards people use than any particular solution
But, to be fair, the particular solution will depend in your environment and there is no universal elegant silver bullet. Given that you happen to be using Python/Django, I recommend Celery. It is an asynchronous task queue for Python, which integrates nicely with Django. And, on top of the features that it gives as an asynchronous task queue, it also has specific features for periodic tasks.
I have personally used the django-celery-beat
integration and it integrates perfectly with Django settings and behaves correctly in distributed environments. If your periodic tasks are related to Django stuff, I strongly recommend to take a look at Celery I started using it only for certain asynchronous mailing and ended up using it for a lot of asynchronous tasks + periodic sanity checks and other web application maintenance stuff.
- [Django]-Django Rest Framework How to update SerializerMethodField
- [Django]-How should I set my DATABASE_URL?
- [Django]-How do I setup a unit test user for django app? The unit test can't login