'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



Solution 1:[1]

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}]'

Solution 2:[2]

You can simply use CTRL+C to quit your server .
else with ubuntu system you can do like below .

prashant@prashant-OptiPlex-3010:~$  ps -eaf|grep runserver
prashant  3445  2805  4 11:20 pts/0    00:00:00 python manage.py runserver
prashant  3446  3445  5 11:20 pts/0    00:00:00 /usr/bin/python manage.py runserver
prashant  3449  3375  0 11:20 pts/1    00:00:00 grep --color=auto runserver
prashant@prashant-OptiPlex-3010:~$ kill 3446
prashant@prashant-OptiPlex-3010:~$ 

Solution 3:[3]

You can write a .bat script in Command Prompt in Windows:

@ECHO OFF
SET /A port=8000
FOR /F "tokens=5" %%T IN ('netstat -ano ^| findstr :%port%') DO (
    SET /A processid=%%T
    TASKKILL /PID %%T /F
)

This will kill all connections using port 8000 (you may started more than one instance of the server running simultaneously`.

SUCCESS: The process with PID 5104 has been terminated.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sherpya
Solution 2 Prashant Gaur
Solution 3 Alfred Wallace