1#! /usr/bin/env bash 2# Stop all BabbleSim processes, finding them from their lock files. 3# Also remove the folder used to keep the locks and fifos 4# 5# A simulation identification string may be provided as the first paramter to 6# this script to limit it to only clean up for that simulation 7 8s=$1 9FOLDER=/tmp/bs_$USER/$s/ 10 11if [ ! -d "$FOLDER" ]; then 12 echo "Not even the folder $FOLDER was found" 13 exit 0 14fi 15 16function remove_folder() { 17 rm -rf -v $FOLDER 18} 19 20FOLDER=`realpath $FOLDER` 21 22LOCKS=`find $FOLDER -name "*.lock"` 23 24if [ ! -n "$LOCKS" ]; then 25 echo "No BabbleSim processes found" 26 remove_folder 27 exit 0 28fi 29 30for LOCK in $LOCKS; do 31 if [ ! -f $LOCK ]; then 32 echo "$LOCK dispeared as we cleaned up" 33 #As we kill components of a simulation other many get unblocked and clean up on their own 34 continue 35 fi 36 PID=`head -n1 $LOCK` 37 START=`tail -n1 $LOCK` 38 DEAD=0 39 kill -0 $PID 2> /dev/null 40 if [[ $? -ne 0 ]] ; then 41 DEAD=1 42 fi 43 if [[ $DEAD -eq 0 ]]; then 44 OTHERSTART=`cat /proc/$PID/stat | cut -d')' -f2 | cut -d ' ' -f21` 45 if [[ ! $OTHERSTART -eq $START ]]; then 46 DEAD=1 47 fi 48 fi 49 if [[ $DEAD -eq 1 ]]; then 50 echo "Found dead process $PID" 51 else 52 echo -n "Found " 53 ps --no-headers --pid $PID --format pid,lstart,command 54 echo "Killing it" 55 kill -9 $PID 56 fi 57 rm -v $LOCK 58done 59 60remove_folder 61