Bash
11 May 2010
 

sv.sh

 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env bash
function echo_and_exit() {
echo $1
exit 1
}
PID_FILE=$1
if [ ! -e $PID_FILE ]; then
echo_and_exit "$PID_FILE not exist"
fi
SLAVE_PID=`cat $PID_FILE`
if [[ ! $SLAVE_PID ]]; then
echo_and_exit "No slave pid in $PID_FILE"
fi
echo "$PID_FILE contains $SLAVE_PID pid"
if [ ! -e "/proc/$SLAVE_PID/fd/1" ]; then
echo_and_exit "Process with specified pid not exist"
fi
MTIME=$(stat -c"%Y" "/proc/$SLAVE_PID/fd/1")
NOW=$(date +%s)
PASSED=$(( $NOW-$MTIME ))
if (( "$PASSED" > 20 )) ; then
kill -9 $SLAVE_PID
echo "Killed $SLAVE_PID"
fi
echo "Passed $PASSED secs"