Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep my Django server running even after I close my ssh session?

Tags:

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.

like image 920
dangerChihuahua007 Avatar asked May 18 '12 16:05

dangerChihuahua007


People also ask

How do I keep Django server running?

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

What is the command to start a job that will keep on running even after closing the session?

Press Ctrl-A then Ctrl-D . This will "detach" your screen session but leave your processes running. You can now log out of the remote box. If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process.


2 Answers

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.

like image 114
Gyscos Avatar answered Sep 18 '22 15:09

Gyscos


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] $ 
like image 37
Rakib Avatar answered Sep 16 '22 15:09

Rakib