Prevent double launch of django commands

The django command is a command that you run with'python manage.py ~'. By default, many useful commands such as shell are provided. Although not explained in detail here, it is possible to create custom commands. And, the custom command etc. are often run as a batch such as crontab, but for some reason I will try not to double start.

custom_command.py


import sys
import commands
from django.core.management.base import BaseCommand

def is_process_exist(process_name):
    """
Returns True if this process is running
    """
    output = commands.getoutput("ps -ef | grep '%s' | grep -v grep | wc -l" % (process_name))
    is_exist = int(output) >= 2

    return is_exist


class Command(BaseCommand):
    def handle(self, *args, **options):
        #Double startup check
        if is_process_exist(__name__.split('.')[-1]):
            sys.exit('duplication!')

        #The following processing ~

Recommended Posts

Prevent double launch of django commands
Summary of frequently used commands of django (beginner)
Prevent double launch in Python (improved version)
Django DB initialization commands
Launch my Django app
Django commands ignore USE_I18N
Impressions of touching Django
[Django series] Basic commands
(Minimal) usage of django logger
Double submit suppression with Django
Organize [Django] commands and roles