Example of a scheduled task using django-crontab, and django-crontab
Today, I plan to add a scheduled task in my Django application to regularly execute some regular check functions. So I thought that using the django-crontab plug-in can satisfy my needs, the following describes how to use this plug-in.
First, use pip to install the django-crontab plug-in.
pip install django-crontab
Create the script and method to be periodically executed. Assume that the Script Name Is cron. py. The content is as follows:
#!/usr/bin/env python# -*- coding: utf-8 -*-def check(): print "hello django-crontab"
Then add the app to the settings. py file of your application.
INSTALLED_APPS = ( ... 'django_crontab',)
Add the CRONJOBS configuration in the settings. py file as follows:
CRONJOBS = [ ('*/1 * * * *', 'cron.check','>>/tmp/test.log')]
Where:
-The first parameter is a cron expression that defines the execution time of a scheduled task.
-The second parameter is the module and function to be executed.
-The third parameter is the path of the log file when the scheduled script is executed.
The scheduled task and script are defined. Let's see how to make them take effect.
First, check the existing cron job in the system.
python manage.py crontab show
Add and modify a cron job
python manage.py crontab add
Delete A cron job
python manage.py crontab remove
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.