How to kill Django runserver sub processes from a bash script?

bash, django, python, shell

Solution

SOLVED

Thanks to this SO question, I've changed my script to this:

#!/bin/bash
SITE=/home/dev/sites/rmx

echo "RMX using siteroot=$SITE"
$SITE/rmx/manage.py runserver &
compass watch $SITE/media/compass/ &
coffee -o $SITE/media/js -cw $SITE/media/coffee &
hamlpy-watcher $SITE/templates/hamlpy $SITE/templates/templates &

trap "kill -TERM -$$" SIGINT

wait

PIDs preceded with the dash operate on the PID group with the `kill` command, and the `$$` references the PID of the bash script itself.

Thanks for the help, me! No problem, self, and hey -- you're awesome.

Problem

I'm working on a Django website where I have various compilation programs that need to run (Compass/Sass, coffeescript, hamlpy), so I made this shell script for convenience: ``` #!/bin/bash SITE=/home/dev/sites/rmx echo "RMX using siteroot=$SITE" $SITE/rmx/manage.py runserver & PIDS[0]=$! compass watch $SITE/media/compass/ & PIDS[1]=$! coffee -o $SITE/media/js -cw $SITE/media/coffee & PIDS[2]=$! hamlpy-watcher $SITE/templates/hamlpy $SITE/templates/templates & PIDS[3]=$! trap "echo PIDS: ${PIDS[*]} && kill ${PIDS[*]}" SIGINT wait ``` Everything except for the Django server shuts down nicely on a `ctrl+c` because the PID of the server process isn't the PID of the `python manage.py runserver` command. Which means everytime I stop the script, I have to find the running process PID and shut it down. Here's an example: ``` $> ./compile.sh RMX using siteroot.... ... [ctrl+c] PIDS: 29725 29726 29728 29729 $> ps -A | grep python 29732 pts/2 00:00:00 python ``` The first PID, `29725`, is the initial `python manage.py runserver` call, but `29732` is the actual dev server process. edit Looks like this is due to Django's auto-reload feature which can be disabled with the `--noreload` flag. Since I'd like to keep the auto reload feature, the question now becomes how to kill the child processes from the bash script. I would think killing the initial python runserver command would do it...

Original source

Related problems