2

I have a django project hosted on an amazon ec2 linux instance. For run my app also when section is close i use gunicorn but i experience some errors and degradation in perfonrmances. When i run command:

python manage.py runserver

from terminal all works great but when section is close app does not work.

How can i run command "python manage.py runserver" for work forever (until i'll kill it) in background also in case of closed session?

I know there is uWSGI but i prefer if possible use directly django native command.

Thanks in advance

AleMal
  • 1,977
  • 6
  • 24
  • 49
  • 5
    Don't do this. The [Django Docs](https://docs.djangoproject.com/en/2.1/ref/django-admin/#runserver) are very specific about `manage.py runsverver`: **DO NOT USE THIS SERVER IN A PRODUCTION SETTING** – Nils Werner Sep 12 '18 at 07:24

3 Answers3

4

What happens here is that the script is interrupted by SIGHUP signal when your session is closed. To overcome this problem, there is a tool called nohup which doesn't pass the SIGHUP down to the program/script it executes. Use it as follows:

nohup python manage.py runserver &

(note the & in the end, it is needed so that manage.py runs in background rather than in foreground).

By default nohup redirects the output in the file nohup.out, so you can use tail -f nohup.out to watch the output/logs of your Django app.

Note, however, that manage.py runserver is not supposed to be used in production. For production you really should use a proper WSGI server, such as uWSGI or Gunicorn.

Mikhail Burshteyn
  • 4,762
  • 14
  • 27
2

You can install and use tmux if you want to run your scripts in background even after closing SSH and mosh connections

$ sudo apt-get install tmux

then run it using command $ tmux a new shell will be opened just execute your command

$ python manage.py runserver 0.0.0.0:8000

0.0.0.0:8000 here will automatically get your allowed hosts. Now you can detach your tmux session to run it in background using CTRL + B and then press D

Now you can exit your terminal but your command keep on running in tmux. Just learn basic commands to use tmux from here

Jagjeet Singh
  • 547
  • 5
  • 11
0

for that, you can use screen just start a new screen and run python manage.py runserver

gagan trivedi
  • 404
  • 4
  • 11