How to run some script regularly in Django

Trigger

With Web application developed as a hobby, "Collect articles from news sites every morning at 7 o'clock" I wanted to do something like that

Overview

  1. Create a new Django command
  2. Make crontab run Django command periodically

procedure

1. Create a new Django command

What is Django Command in the first place? python manage.py runserver python manage.py migrate What follows python manage.py like

The above two are the original commands Actually, you can make this command yourself For details from official

First, create a management directory under the app 1 directory Create a commands directory under the management directory You don't have to create __init__.py

Create some script under the commands directory This will be the new Django command In other words python manage.py something ` If you enter, the script in "something" will be executed

project
├── App 1
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│ ├── management ← I will make a new one
│   │   ├── __init__.py
│ │ └── commands ← I will make a new one
│   │       ├── __init__.py
│ │ └── Something.py ← I'll make a new one
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── App 2
└── manage.py

2. Describe the contents of the command

Now, the situation where App 1/management/commands/something .py is created Placement is OK Next, you need to tell the contents, "This is a Django command." The minimum required description for that is as follows

sample.py


from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        """Write your favorite script here"""
        pass

3. Write in crontab

Edit with crontab -e

#Do something every morning at 7am
#Execution result is log.Record in txt
0 7 * * * /Absolute path/python3 /Absolute path/manage.py something> /Absolute path/log.txt 2>&1

When entering the command you want to execute with crontab, it is better to specify it with an absolute path Especially when using python in a virtual environment

Details about crontab

Recommended Posts

How to run some script regularly in Django
How to reflect CSS in Django
How to run TensorFlow 1.0 code in 2.0
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to run a Maya Python script
How to implement Rails helper-like functionality in Django
How to reflect ImageField in Django + Docker (pillow)
How to run CNN in 1 system notation in Tensorflow 2
How to run Leap Motion in non-Apple Python
How to create a Rest Api in Django
How to run the Ansible module added in Ansible Tower
How to get multiple model objects randomly in Django
How to run python in virtual space (for MacOS)
How to run tests in bulk with Python unittest
A memorandum to run a python script in a bat file
How to use bootstrap in Django generic class view
How to run setUp only once in python unittest
How to upload files in Django generic class view
How to use Decorator in Django and how to make it
How to run Django on IIS on a Windows server
How to reference static files in a Django project
How to run Notepad ++ Python
How to develop in Python
How to write custom validations in the Django REST Framework
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to use Laravel-like ORM / query builder Orator in Django
How to run a Python program from within a shell script
Don't lose to Ruby! How to run Python (Django) on Heroku
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
[Django] How to give input values in advance with ModelForm
How to generate a query using the IN operator in Django
In Django, how to abbreviate the long displayed string as ....
Run a multi-line script in a PDB
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
[Django] How to test Form [TDD]
How to write soberly in pandas
Errors related to memcached in django
How to use SQLite in Python
[EC2] How to run selenium webdriver
How to run matplotlib on heroku
How to convert 0.5 to 1056964608 in one shot
How to get started with Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to write Django1.9 environment-independent wsgi.py
How to handle Japanese in Python
How to log in to Docker + NGINX
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to call PyTorch in Julia
How to install OpenCV on Cloud9 and run it in Python
[Django] How to read variables / constants defined in an external file
How to deploy a Django app on heroku in just 5 minutes
How to not run some tests by default using pytest's mark
How to determine if a shell script was started in bash