Using Django-crontab to implement timed tasks

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

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

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.