#!/bin/bash

# @file:    $RCSfile: stop_fe.sh,v $
# @brief:   $系统应用停止进程脚本 
# @version: $Revision: 1.5 $
# @date:    $Date: 2017/02/28 08:40:43 $
# @author:  $Author: zhanchengguo $
# @state:   $State: Exp $
# @latest:  $Id: stop_fe.sh,v 1.5 2017/02/28 08:40:43 zhanchengguo Exp $

check_log_file()
{
if [ -n "$1"  ] ; then
    FILE_SIZE=0
    FILE_SIZE=`du $1 | awk '{print $1}'`
    if [ $FILE_SIZE -ge 5120 ]
    then
        if [ -f "$1".3 ]; then 
            rm -f "$1".3
        fi
        if [ -f "$1".2 ]; then 
            mv "$1".2 "$1".3
       fi
        if [ -f "$1".1 ]; then 
            mv "$1".1 "$1".2
        fi
        mv "$1" "$1".1
    fi
fi
}

LOGFILE="/FeProject/dat/log/start_fe.log"
check_log_file $LOGFILE

echo "" ; echo "" 
echo "****** `date "+%F %R:%S"` Stop Processes ******"
echo "" >>"$LOGFILE"
echo "" >>"$LOGFILE"
echo "****** `date "+%F %R:%S"` Stop Processes ******" >>"$LOGFILE"

RUNTIME_CF_FILE="/FeProject/etc/runtime.cf"

#cat $RUNTIME_CF_FILE  |tac > /tmp/runtime.cf
sed '1!G;h;$!d' $RUNTIME_CF_FILE > /tmp/runtime.cf

echo "Using config file name: $RUNTIME_CF_FILE"
echo "Using config file name: $RUNTIME_CF_FILE">>"$LOGFILE"

while read LINE ; do
	if [ -n "$LINE"  ] ; then
		if [  ${LINE:0:1} != "#" ]; then			
			BINPATH=`echo $LINE | awk -F '^' '{printf $1}'`
			BINPATH=`echo "$BINPATH" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $BINPATH
			BIN=`echo $LINE | awk -F '^' '{printf $2}'`
			BIN=`echo "$BIN" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $BIN
			SPECIAL=`echo $LINE | awk -F '^' '{printf $4}'`
			SPECIAL=`echo "$SPECIAL" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $SPECIAL
			
			SLEEPSEC=`echo $LINE | awk -F '^' '{printf $5}'`
			SLEEPSEC=`echo "$SLEEPSEC" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $SLEEPSEC
			STARTFLAG=`echo $LINE | awk -F '^' '{printf $7}'`
			STARTFLAG=`echo "$STARTFLAG" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $STARTFLAG		
			
			if [ "`echo $STARTFLAG`" != "IGNORE_START" ]; then
#				echo ""
#				echo "" >>"$LOGFILE"
                		CMD_STR="  ps -w | grep `echo "'"``echo $BIN``echo "'"` | grep -v 'grep'"
				BIN2=`echo $BIN` 
				BIN3=`echo $BIN |awk '{print$1}'`
#				CMD_STR="killall `echo $BIN | awk '{print$1}'` 2>&1 "
				PIDSTR=`ps -ef | grep "$BIN2" | grep -v 'grep'`				
                		if [ -n "$PIDSTR"  ] ; then
				    if [ "$BIN3" == "fe_main" ]; then
					CMD_STR="    sleep 3 "
				      echo "$CMD_STR"
				      echo "$CMD_STR" >>"$LOGFILE"
				      $CMD_STR
				    fi

				    CMD_STR="kill -15 `echo $PIDSTR | awk '{print$2}'` "
				    echo "  Kill process:  \"$BIN\" with PID `echo $PIDSTR | awk '{print$2}'`"
				    echo "  Kill process:  \"$BIN\" with PID `echo $PIDSTR | awk '{print$2}'`" >>"$LOGFILE"
				    echo "  Executing:  $CMD_STR"
				    echo "  Executing:  $CMD_STR" >>"$LOGFILE"
				    CMD_RESULT=`$CMD_STR`
				    if [ -n "$CMD_RESULT"  ] ; then	
			 	        echo "$CMD_RESULT"
			 	        echo "$CMD_RESULT" >>"$LOGFILE"
			 	    fi
				    
			 	else
			 	    echo "  Process \"$BIN\" not running."
			 	fi
			fi
		fi
	fi		
done < /tmp/runtime.cf

DT=`date "+%F %R:%S.%N"`
echo "****** ${DT:0:23} Stop Processes Succcessfully ******" 
echo "" >>"$LOGFILE"
echo "****** ${DT:0:23} Stop Processes Succcessfully ******"  >>"$LOGFILE"
