When I run a Django testing server in a terminal it sometimes gets stuck and it can’t be killed with CTRL+C. Or I’m using SSH client in background mode for a secure proxy tunnel and I need to restart it from time to time. Searching manually for PIDs with
grep and killing them is a lengthy process.
Following piece of a bash code does it automatically. Process determined by the CMD variable content will be first killed and then executed.
#!/bin/bash CMD="python manage.py runserver 0.0.0.0:8000 --traceback" # first kill possible background server process PIDS=`ps -fe | grep "$CMD" | grep -v grep | sed 's/ \+/ /g' | cut -d' ' -f2` if [ -n "$PIDS" ]; then echo killing: $PIDS kill $PIDS fi $CMD $@