32

I figured out how to run my Django application via sudo python /home/david/myproject/manage.py runserver 68.164.125.221:80. However, after I quit terminal, the server stops running.

I tried to run this process in the background, but the server just shuts down quickly after I execute sudo python /home/david/myproject/manage.py runserver 68.164.125.221:80 &.

How do I keep my Django application running even after I quit my ssh session in terminal?

PS - Sorry if this question strikes you as elementary. Such sillyness ensues when a front-end javascript programmer must turn into a server administrator in break-neck speed.

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • 4
    Just remember runserver is only for development... – Yuji 'Tomita' Tomita May 18 '12 at 16:42
  • Wait, why is it only for development? I read that too in Django documentation, but what are the drawbacks? I think it would work fine. – dangerChihuahua007 May 18 '12 at 16:47
  • 11
    Don't. Use. Runserver. In. Production. Ever. It's single-threaded. It's insecure. It is not optimized. At all. It is solely and purely a *convenience* to allow you test your code in development. It was never designed nor intended to be used for anything beyond that. – Chris Pratt May 18 '12 at 16:59
  • Wow, thank you. Those are important priorities. What, by the way, are the repercussions of the server being single-threaded? – dangerChihuahua007 May 18 '12 at 17:26
  • 4
    @DavidFaux, only ever serving one client at a time. Even if you only ever had 1 hit per second exactly though it's just plain slow. – Yuji 'Tomita' Tomita May 18 '12 at 18:18
  • Oh yikes, I certainly want more than 1 client to be able to visit my site concurrently. Thank you, I did not know that. – dangerChihuahua007 May 18 '12 at 19:11
  • Are there well-known patches for making the server multithreaded though? Such as http://twigstechtips.blogspot.com/2011/05/django-run-development-server-in-multi.html – dangerChihuahua007 May 18 '12 at 19:18

3 Answers3

35

Meet screen.

Connect through ssh, start screen. This open a virtual console emulator on top of the one provided by ssh. Start your server there.

Then press Ctrl-a, then d. This detach the screen session, keeping it running in the background.

To [R]e-attach to it, use screen -r.

If screen is not installed and you can't install it, you can also start an application in the background by adding a & to the command, as you tried. But you should not close the terminal window then ; just disconnect, with the bash command exit, or Ctrl-d.

The advantage of screen is that you can still read the output from the server, in case there is an error or anything.

Screen is a really powerful tool, with many more commands. You can add a new virtual window with Ctrl-a, then c (for Create) ; switch through windows with Ctrl-a, then n (next) or p (previous), ...

But you need it to be installed to use it. Since you seem to have root access, this shouldn't be a problem.

EDIT: tmux is another great solution for the same use-case.

Gyscos
  • 1,772
  • 17
  • 22
  • Thanks! I'll check out screen after installing it. I'm getting a weird `!!! no tgetent - no screen` error upon making the installation, but I'll figure it out. – dangerChihuahua007 May 18 '12 at 16:56
  • Your distribution probably has a package pre-configured ready to be installed. Did you use that ? For debian or ubuntu, it'd be apt-get install screen. That way, it will also install required packages. – Gyscos May 18 '12 at 17:02
  • Ah, thanks! I fixed the issue by executing `sudo apt-get install libncurses5-dev` to get some required c libraries. Wow, screen works great! I just ran the server in a detached emulated terminal and closed my ssh connection. The server is still running :D I verified the process with `pstree` too. I might not use the Django runserver per the comments above, but this answer certainly solves my original question and taught me something new. Thank you :D – dangerChihuahua007 May 18 '12 at 17:14
22

Use screen to create a new virtual window, and run the server there.

$ screen
$ python manage.py runserver

You will see that Django server has started running.

Now press Ctrl+A and then press the D key to detach from that screen. It will say:

$ [detached from ###.pts-0.hostname]

You can now safely logout from your terminal, log back in to your terminal, do other bits of coding in other directories, go for a vacation, do whatever you want.


To return to the screen that you have detached from,

$ screen -r

To kill the django server now, simply press Ctrl+C like you would've done normally.


To terminate this current screen instead of detaching from this screen, use Ctrl+D. It will say:

$ [screen is terminating]
$
Rakib
  • 12,376
  • 16
  • 77
  • 113
9

Use nohup. Change your command as follows:

nohup sudo python /home/david/myproject/manage.py runserver 68.164.125.221:80 &
ayan_2587
  • 833
  • 2
  • 11
  • 21