welcome to the world of…

Looks like a small bulb used to indicate something unusual, like a malfunction.

Run complex command/process with killing previous

Filed under: Uncategorized — Tags: , , , , — admin @ 2013-11-07 11:05

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 ps and 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 $@

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.