If you are our UNIX or Linux user, you must have heard of cron (Cron Jobs). It’s one of the most useful tools in Unix systems although usually used in sysadmin jobs but still, you might need one too !?
According to Wikipedia:
The software utility Cron is also known as a cron job is a time-based job scheduler in Unix-like computer operating systems. Users who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
Today we’ll talk about setting up cron jobs in Django.
Firstly it depends on the specific use case if you even need a cron job. You might need a distributed task queue like Celery for your case.
If you are confused among the two, check out this [StackOverflow](https://stackoverflow.com/questions/16232572/distributed-task-queues-ex-celery-vs-crontab-scripts#:~:text=What celery brings to the,a minimum of one minute.) answer.
We need the package django-crontab for running cron jobs.
- Install the package and add it as a app inside installed apps
pip install django-crontab
- Add the app to installed apps. I also have a
test_appapp inside my project for demonstration. Also, add the
CRONJOBSsetting inside settings.py
CRONJOBS = [ ('*/5 * * * *', 'test_app.cron.my_scheduled_job') ] # /5 represent every 5 minutes # my_scheduled_job is the jobs we'll add
INSTALLED_APPS = ( 'django_crontab', 'test_app', ... )
- Create a
[cron.py](<http://cron.py>)inside the test_app and add the following –
def my_scheduled_job(): pass
- finally run this command to add all defined jobs from CRONJOBS to crontab (of the user which you are running this command with):
python manage.py crontab add
- show current active jobs of this project:
python manage.py crontab show
- removing all defined jobs is straight forward:
python manage.py crontab remove
Create a model object every minute (Simple)
from .models import Test # importing the test model def my_scheduled_job(): Test.objects.create(name='test')
- You can adjust the timing in the
CRONJOBSarray we added in
Check if a document is expired ? (Intermediate)
# test_app.models - Add a test Document model class Document(models.Model): uploaded_file = models.FileField(upload_to='images/') expiration_date = models.DateField() expired = models.BooleanField(default=False) updated = models.DateTimeField(auto_now=True) created = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.id)
# test_app.cron - Check today's date on the file from .models import Document from datetime import datetime def document_expired_check(): today = datetime.today.strftime('%Y-%m-%d') qs = Document.objects.filter(expired=False) for doc in qs: exp = doc.expiration_date.strftime('%Y-%m-%d') if exp < today: doc.expired=True doc.save()
# settings.py - Add the job with a check performed every minute CRONJOBS = [ ('*/5 * * * *', 'test_app.cron.my_scheduled_job'), ('*/1 * * * *', 'test_app.cron.document_expired_check') ]
- You’ll need to run the
cronjobcommand above to add it.
You can try out more advanced examples if you need to but this will be all for this article.
Author – Animesh Singh