[Swift-commit] r7251 - trunk/bin

davidk at ci.uchicago.edu davidk at ci.uchicago.edu
Wed Oct 30 12:16:55 CDT 2013


Author: davidk
Date: 2013-10-30 12:16:55 -0500 (Wed, 30 Oct 2013)
New Revision: 7251

Modified:
   trunk/bin/swift-service
Log:
Add -stop option, kill pids and their children 


Modified: trunk/bin/swift-service
===================================================================
--- trunk/bin/swift-service	2013-10-30 14:36:17 UTC (rev 7250)
+++ trunk/bin/swift-service	2013-10-30 17:16:55 UTC (rev 7251)
@@ -1,5 +1,13 @@
 #!/bin/bash
 
+# Determine the location of needed files
+export SWIFT_BIN="$( cd "$( dirname "$0" )" && pwd )"
+export WORKER="$SWIFT_BIN/worker.pl"
+export PID_FILE="$HOME/.swift/.coaster-service-pids"
+export COASTER_SERVICE="$SWIFT_BIN/coaster-service"
+export LOG="start-coaster-service.log"
+mkdir -p "$HOME/.swift" || crash "Unable to create $HOME/.swift"
+
 # Report a problem and exit
 crash()
 {
@@ -23,38 +31,64 @@
    done
 }
 
+# Write command to log, run command, wait for completion
 run_command()
 {
    command="$@"
-   echo "Running $command" >> $LOG
+   echo "Running command $command" >> $LOG
    $command >> $LOG 2>&1
 }
 
+# Write command to log, run in background, record PID
 run_command_bg()
 {
    command="$@"
-   echo "Running $command" >> $LOG
+   echo "Running background command $command" >> $LOG
    $command >> $LOG 2>&1 &
    echo $! >> $PID_FILE
 }
 
+# Stop the coaster service
+stop_service()
+{
+   echo "Stopping Swift service..."
+   if [ -f "$PID_FILE" ]; then
+      for PID in $( cat $PID_FILE )
+      do
+         PID_COMM=$( ps -p $PID -o comm|sed 1D 2>/dev/null )
+
+         # Kill child processes of $PID
+         ps -u $USER -o "pid,ppid"|sed 1d | while read PROC
+         do
+            PROC_PID=$( echo $PROC | awk '{print $1}' )
+            PROC_PPID=$( echo $PROC | awk '{print $2}' )
+            if [ $PROC_PPID == $PID ]; then
+               run_command kill $PROC_PID
+            fi
+         done
+
+         # Get process name
+         if [ -z "$PID_COMM" ]; then
+            PID_COMM="$pid"
+         fi 
+
+         echo "Killing $PID_COMM ($pid)"
+         run_command kill $pid
+      done
+   fi
+   rm $PID_FILE
+}
+
 # Parse command line arguments
 while [ $# -gt 0 ]; do
    case $1 in
       -conf) CMDLN_CONF=$2; shift 2;;
       -start) ;;
-      -stop) ;;
+      -stop) stop_service; exit 0;;
       *) echo "Do not recognize command line option: $1" 1>&2; exit 1;;
    esac
 done
 
-# Determine the location of needed files
-export SWIFT_BIN="$( cd "$( dirname "$0" )" && pwd )"
-export WORKER="$SWIFT_BIN/worker.pl"
-export PID_FILE="$HOME/.swift/.coaster-service-pids"
-export COASTER_SERVICE="$SWIFT_BIN/coaster-service"
-export LOG="start-coaster-service.log"
-mkdir -p "$HOME/.swift" || crash "Unable to create $HOME/.swift"
 
 # Import settings
 if [ -f "$CMDLN_CONF" ]; then
@@ -85,6 +119,10 @@
    crash "Unable to find $COASTER_SERVICE!"
 fi
 
+if [ -z "$TMPDIR" ]; then
+   TMPDIR=$PWD
+fi
+
 # Create files for storing port info, if needed
 if [ -z "$LOCAL_PORT" ]; then
    LOCAL_PORT_FILE=$( mktemp $TMPDIR/XXXXXX )




More information about the Swift-commit mailing list