crontab
Django can use third-party libraries such asdjango-crontab
to realize the scheduling of timing tasks. This library allows you to specify the execution time of a task using syntax similar to the crontab file format.
GitHub address:/kraiz/django-crontab
Install django-crontab
pip install django-crontab
Register an application
Register django-crontab application in file
INSTALLED_APPS = [ 'django_crontab', # Timing tasks]
Timed time format
django-crontab uses the commonly used cron scheduler under Linux for task scheduling. For the specification of time format, it follows the standard crontab syntax and is divided into five fields, from left to right:
* * * * * - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) -------------d | | | +------- month (1 - 12) -------------m | | +--------- day of the month (1 - 31) -------------D | +----------- hour (0 - 23) -------------H +------------- min (0 - 59) -------------M
illustrate:
M: Minutes (0-59) are expressed by * or */1 per minute.
H: hours (0-23) (0 means 0 points)
D: Day (1-31)
m: Month (1-12)
d: Days within one week (0~6, 0 is Sunday)
Asterisk (*): All possible values, such as if the month field is an asterisk, it means that the command operation is executed every month after the constraints of other fields are met.
Comma (,): You can specify a list range with comma separated values, for example: 1,2,5,7,8,9
Medium bar (-): You can use the medium bar between integers to represent an integer range, for example: 2-6 represents 2, 3, 4, 5, 6
Forward slash (/): The interval frequency of time can be specified using forward slash, for example: 0-23/2 means that it is executed every two hours. At the same time, the forward slash can be used with the asterisk, such as */10. If used in the minute field, it means that it will be executed every ten minutes
Note: All values must be within the corresponding range, otherwise they will be considered invalid.
Timer example
0 */1 * * * # Execute every 1 hour0 10 * * * # Execute at 10 o'clock every day30 19 * * * # Execute at 19:30 every day0 10 * * 1 # Execute every Monday at 10 o'clock30 17 * * 5 #Enforced at 17:30 every Friday0 10 1 10 * # Execute at 10:00 on October 1st every year0 5,17 * * * # Execute tasks at 5:00 and 17:00 every day0 3 * * * # Indicates that it will be executed at 3 a.m. every day*/5 * * * * # Indicates that it will be executed every 5 minutes0 0 1,15 * * # Indicates midnight on the 1st and 15th of each month30 8 1-7 * * # Indicates the previous month7In the sky,Daily morning8:30implement
Set up timing tasks
Timed tasks are divided into three parts: task time, task method, task log, and in file configuration:
CRONJOBS = [ # Execute every 1 minute # users: Application name, task is py file name, task_job is the function in the file ('*/1 * * * *', '.task_job', '>> ' + (BASE_DIR, 'logs/')), # Run every 5 minutes ('*/5 * * * *', '.my_scheduled_job'), ]
@Symbol Method
django-crontab supports the use of the @ symbol to quickly specify some common usages, such as:
@reboot:Run once at system startup @yearly or @annually:each year1moon1day 0:00 Run once @monthly:每moon1day 0:00 Run once @weekly:每周day 0:00 Run once @daily or @midnight:every day0:00 Run once @hourly:每小时Run once
@yearly or @annually: means it is executed once a year, which is equivalent to0 0 1 1 *
CRONJOBS = [ ('@yearly', '.my_scheduled_job'), ]
@monthly: means that it is executed once a month, which is equivalent to0 0 1 * *
CRONJOBS = [ ('@monthly', '.my_scheduled_job'), ]
@weekly: It means it is executed once a week, which is equivalent to0 0 * * 0
CRONJOBS = [ ('@weekly', '.my_scheduled_job'), ]
@daily or @midnight: means that it is executed once every day at midnight, which is equivalent to0 0 * * *
CRONJOBS = [ ('@daily', '.my_scheduled_job'), ]
@hourly: means that it is executed once an hour, which is equivalent to0 * * * *
CRONJOBS = [ ('@hourly', '.my_scheduled_job'), ]
Solve crontab Chinese problem
In a timing task, if non-English characters appear, a character exception error will occur
CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'
Manage timing tasks
1. Perform timing tasks
# Add timing tasks to the systempython crontab add
2. Cancel the timing task
# Remove timing taskspython crontab remove
3. Query Activate the Timed Task
# Show activated timing taskspython crontab show
Notice
Not available for Windows platforms.
-crontab uses UTC time by default, which may be different from the current system time zone.
You can specify the CRONTAB_TIMEZONE option in the file to set the time zone, for example:
Set the time zone to East Eighth District (GMT+8)
CRONTAB_TIMEZONE = ‘Asia/Shanghai'
This is the end of this article about the implementation of Django-crontab timing tasks. For more related content of django-crontab timing tasks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!