Example of a scheduled task using django-crontab, and django-crontab

Source: Internet
Author: User
Tags install django pip install django

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.