Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Django runserver alive when SSH is closed

I haven't yet been able to get Apache working with my Django app, so until I do get it working, I'm using runserver on my Linux server in order to demo the app. The problem is that whenever I close the SSH connection to the server, runserver stops running. How can I keep runserver running when I say put my laptop to sleep, or lose internet connectivity?

P.S. I'm aware that runserver isn't intended for production.

like image 920
Daniel Avatar asked Aug 14 '15 13:08

Daniel


3 Answers

Using Screen

You can run runserver in a screen session. After you detach from that session it will keep running. Login via ssh and start a screen session via

screen

It will look like your usual terminal. Now, run the server

python manage.py runserver 8080

After this, you can detach from the session using Ctrl+a Ctrl+d. Now your app should be available even after you quit your ssh session.

If you want to cancel the runserver, you can re-atach to your screen session. Get a list of existing sessions with

screen -ls

There is a screen on:
    10989.pts-1.hostname (Detached)
1 Socket in /run/screens/S-username.

Then, you can reatach with the command

screen -R 10989

Using nohup

Again, after login into your server, start the runserver with

nohup python manage.py runserver 8080 &

All output the runserver writes (like debug info and so on) will be written to a file called nohup.out in the same folder.

To quit the server after using nohup you need to keep the process id (pid) shown or find the pid afterwards with ps, top or any other tool.

like image 182
chris-sc Avatar answered Oct 11 '22 03:10

chris-sc


Since runserver isn't intended to be ran in production/only for development there is no way to to this built in.

You will need to use a tool like tmux/screen or nohup to keep the process alive, even if the spawning terminal closes.

like image 20
Chris Schäpers Avatar answered Oct 11 '22 04:10

Chris Schäpers


$ nohup python manage.py runserver &

nohup makes command ignore hangup signal and & puts in to background disconnecting from stdout

like image 25
user996142 Avatar answered Oct 11 '22 04:10

user996142