0

I am running a django webserver with default manage.py file, how can i daemonize it using svc daemontools

Content of my run file in daemontools is

#!/bin/bash
exec setuidgid <myuser> /usr/bin/python <path/to/manage.py> runserver 2>&1

Content of manage.py

import os
import sys

if __name__ == "__main__":
  os.environ.setdefault("DJANGO_SETTINGS_MODULE","settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

The child process(manage.py runserver) of svscanboot forks off another process in execute_from_command_line (imported from django.core.management) function, which cannot be controlled through svc commands. Below is the process tree for svscanboot.

/bin/sh /usr/bin/svscanboot
\_ svscan /etc/service
   \_ supervise myapp
      \_ /usr/bin/python </path/to/manage.py> runserver
          \_ /usr/bin/python </path/to/manage.py> runserver

Now if i execute svc -d it sends the TERM signal to the first runserver process, and as a result just that gets killed and the second process becomes an orphan process which cannot be controlled through svc commands.

So how do i control the entire process tree of the child process using daemontools?

ajays20078
  • 368
  • 1
  • 3
  • 10

1 Answers1

0

This can be solved by running django server with a --noreload option.

Django spawns another process only when --noreload is not passed and this is meant for dev environment only.

https://github.com/django/django/blob/master/django/utils/autoreload.py#L290

In non-dev environment you would not want the django server to get reloaded, as you wont be changing code in such environments. So running the server with --noreload solves the issue.

However it is not recommended to run default django server in production.

ajays20078
  • 368
  • 1
  • 3
  • 10
  • No it does not. It's not supposed to be used in production. You've had that comment from a leading authority on the subject. – e4c5 May 17 '16 at 10:55