- # TODO wait for process to finish until timeout, then kill
- echo Stopped $APP with pid $PID
+
+ # wait 5 min for application to shutdown, then kill it
+ TIMEOUT=$((5*60))
+ BEGIN=$(date +%s)
+ while kill -0 $PID &> /dev/null
+ do
+ sleep 1
+ NOW=$(date +%s)
+ DURATION=$(($NOW-$BEGIN))
+ if [ $DURATION -gt $TIMEOUT ]; then
+ kill -9 $PID
+ echo Forcibly killed $APP with pid $PID
+ RETVAL=1
+ fi
+ done
+
+ # remove pid file
+ rm -f $PID_FILE
+ return $RETVAL
+
+# timeout is only available in EL6
+# timeout 5m sh << EOF
+#while kill -0 $PID &> /dev/null; do sleep 1; done
+#EOF
+# TIMEOUT_EXIT=$?
+# if [ $TIMEOUT_EXIT -eq 124 ];then
+# kill -9 $PID
+# RETVAL=1
+# echo Killed $APP with pid $PID
+# else
+# echo Stopped $APP with pid $PID
+# fi
+# rm -f $PID_FILE
+# return $RETVAL
+}
+
+status() {
+ if [ -f $PID_FILE ];then
+ PID=`cat $PID_FILE`
+ else
+ echo $APP is not running
+ return $RETVAL
+ fi
+ kill -0 $PID &> /dev/null
+ PID_EXISTS=$?
+ if [ $PID_EXISTS -eq 0 ]; then
+ echo $APP is running with pid $PID ...
+ else
+ echo No $APP process with pid $PID, removing $PID_FILE
+ rm -f $PID_FILE
+ fi