Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I quit a Django development server with a bash script?

I wrote a bash script to start a Django development sever, however, I want to be able to quit the server while it's running with a bash script as well. I'm writing a web app for Koding.com that starts a Django process in an online terminal linked to the user's personal VM by running a bash script with a press of a button, and I want users to be able to end the process with a press of a button as well. I know that control C would end the process, but I haven't been able to find out how to do that in a bash script. How could I go about doing this? Thanks! enter image description here

UPDATE: I ultimately combined my selected answer for this question and this answer from another question I asked (how to run unix commands in django shell) to solve my problem: https://stackoverflow.com/a/22777849/2181017

like image 564
PuffySparrow Avatar asked Apr 01 '14 02:04

PuffySparrow


2 Answers

You can kill the process listening on 8000/TCP:

fuser -k 8000/tcp

The fuser command shows which processes are using the named files, sockets, or filesystems and the -k option kills them as well.

Wait, just to clarify, there's no way to temporarily exit that django shell thing so that I could run a unix command?

When you start the development server it will run in foreground, blocking the prompt. You can pause the task that is running in foreground by hitting CTRL+Z, and then send the task to background running the bg command (I assume you are running the bash shell or a lookalike). The jobs command will list paused tasks or tasks running in background. You can bring a task to foreground using the fg command.

Please refer to the bash man page (man bash).

like image 85
Paulo Scardine Avatar answered Oct 04 '22 10:10

Paulo Scardine


what about GNU screen? http://www.gnu.org/software/screen/

screen
./manage.py runserver 0.0.0.0:8000
ctrl + a then d (this will detach from screen)
exit (from the shell)

when you need to stop, login back in the shell

screen -r
ctrl + c
exit (from screen)
exit (from the shell)

put this in ~/.screenrc for a nice screen statusline

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
like image 31
sherpya Avatar answered Oct 04 '22 08:10

sherpya