How to Make Django Methods automatically run regularly

Source: Internet
Author: User

Implementation ideas:
1. First, write the Django method to be automatically executed as Django command
2. add your own Django command to cron and use the cron service for regular execution.

Part1 Add a custom Django command in the Django Project
1. the application we created is called MyApp. First, in this app directory, we need to create a new management Directory, which should include __init __. PY (the content is empty for packaging) and commands directory, and then include __init __. py and mycommand. PY, where mycommand. py is the method used for custom command. The tree structure of the directory is as follows:

myapp/    __init__.py    models.py    management/        __init__.py        commands/            __init__.py            mycommand.py    tests.py    views.py

 

2. Write the command content mycommand. py. A simple example is as follows:

from django.core.management.base import BaseCommand, commandError           class Command(BaseCommand):    def handle(self, *args, **options):                 print "hello world"

 

We associate the basecommand class and use the command class and handle method as the framework. As shown above, this command implements printing "Hello World ".

Through the above, we can use Python manage. py mycommand to run our own Django command.

For more details, see here.

Part2 uses cron to regularly execute Django command

1. enable/disable/restart cron in Ubuntu
The configuration file is usually/etc/init. d/cron.
Start: sudo/etc/init. d/cron start
Close: sudo/etc/init. d/cron stop
Restart: sudo/etc/init. d/cron restart
Sudo service cron restart
Reload configuration: sudo/etc/init. d/cron reload

You can run the PS aux | grep cron command to check whether cron has been started.

2. added a Cron task.
We recommend that you use the crontab-u username-e command to add custom tasks (The Cron file of the corresponding user in/var/spool/cron is edited, the crontab file under/var/spool/cron cannot be directly created or modified. The crontab file is obtained through the crontab command ).

A. directly execute the command line
Print a string "Hello World" every five seconds and save it to the/home/linjiqin/helloworld.txt file. The Cron format is as follows:
*/5 ****? Echo "Hello World">/home/linjiqin/helloworld.txt

B. Shell files
Call the/home/linjiqin/helloworld. Sh file every 5 seconds. The Cron format is as follows:
*/5 ****? /Home/linjiqin/helloworld. Sh

The file/home/linjiqin/helloworld. Sh contains the following content:

#!/bin/shcd /home/linjiqin/echo "Hello World" >> HelloWorld.txt

 

Note: Remember to restart after editing, otherwise it will be invalid.
Sudo service cron restart
Sudo service cron status
Crontab-L # view Cron
Crontab-e # modify Cron

Common cron examples:

Run the command once every 5 seconds: */5 ****? Run Once every 1 minute: 0 */1 ***? Execute once at every day: 0 0 23 **? Run the command once every morning: 0 0 1 **? Execute once at on the first day of every month: 0 0 1 *? Run Once at on the last day of each month: 0 0 23 L *? Once every Sunday at AM: 0 0 1? * L at 26, 29, and 33: 0, 26, 29, 33 ***? Every day at, and: 0 0 **?

 

How to Make Django Methods automatically run regularly

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.