Today I'm going to add a timed task to my Django application to perform some periodic checks, so I thought of using the Django-crontab plugin to meet my needs, and here's how to use this plugin.
First install the Django-crontab plug-in using PIP
Pip Install Django-crontab
Create scripts and methods to be executed on a regular basis, assuming that the script name is cron.py, which reads as follows:
#!/usr/bin/env python
#-*-coding:utf-8-*-
def check ():
print "Hello Django-crontab"
Then add this app to your application's settings.py file.
Installed_apps = (
... ')
Django_crontab ',
)
Also add the Cronjobs configuration in the settings.py file as follows:
Cronjobs = [
(' */1 * * * * *, ' cron.check ', ' >>/tmp/test.log ')
]
which
-the first parameter is a cron expression that defines the execution time of the scheduled task.
-the second parameter is the module and function to execute.
-the third parameter is the path to the log file when the timed script is executed.
The timed tasks and scripts are defined, and here's how to get them into effect.
First look at the existing cron job in the system
Adding and modifying a cron job
Delete a cron job