# 配置 APPS NAMES 即可
#!/bin/bash
#####################################################Environment Setting#######################################################
#程序代码数组
APPS=(MIR SM MGW IMR TRANSCODING)
#程序名称数组
NAMES=(路由模块 会话管理模块 网关模块 智能服务模块 文件转换模块)
TAB2="----------"
# 统一启动停止前缀
COMMANDPREFIX="java -Xmx1024M -Xms1024M -jar ";
#PATHS=(./ ./ ./ ./ ./)
BASEPATH=$(cd `dirname $0`; pwd)
start(){
local APPNAME=
local NAME=
local CLASSNAME=
local CLASSNAME_LOGS=
local PROJECTDIR="$BASEPATH"
local command="sh service.sh start"
local cmd2="$1"
local cmd2ok=0
local cnt=0
local okcnt=0
#local C_PID="0"
#for i in `seq 0 22`
if[ -n "$2"];
then
local LOGDIRS="${PROJECTDIR}/logs/"
if [ ! -d "$LOGDIRS" ]; then
mkdir "$LOGDIRS"
fi
fi
echo "---------------------------开始启动服务..."
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME="${APPS[$i]}.jar"
#PROJECTDIR=${PATHS[$i]}
if [ "$cmd2" == "all" ] || [ $(echo "$cmd2" | tr '[A-Z]' '[a-z]') == $(echo "$APPNAME" | tr '[A-Z]' '[a-z]') ]; then
echo "$APPNAME---$NAME:"
cmd2ok=1
C_PID="0"
cnt=0
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
#do
#C_PID=$(ps --no-heading $pid | wc -l)
#if [ "$C_PID" == "1" ]; then
if [ -n "$PID" ]
then
echo "${TAB2}己经运行,PID=$PID"
#okcnt=$(($okcnt+1))
else
cd $PROJECTDIR
if[ -n "$2"];
then
#rm -f $PROJECTDIR/$(echo $APPNAME | tr '[A-Z]' '[a-z]').out
echo "${TAB2}日志将清空,并且重新生成:$LOGDIRS$(echo $APPNAME | tr '[A-Z]' '[a-z]').out"
#rm -f $PROJECTDIR/$LOGDIRS$(echo $APPNAME | tr '[A-Z]' '[a-z]').out
>$LOGDIRS$(echo $APPNAME | tr '[A-Z]' '[a-z]').out
command="nohup ${COMMANDPREFIX}$CLASSNAME"
#exec $command >> $PROJECTDIR/$(echo $APPNAME | tr '[A-Z]' '[a-z]').out 2>&1 &
#exec $command > /etc/null 2>&1 &
exec $command > $LOGDIRS$(echo $APPNAME | tr '[A-Z]' '[a-z]').out 2>&1 &
else
command="nohup ${COMMANDPREFIX}$CLASSNAME"
exec $command > /etc/null 2>&1 &
fi
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
#do
# C_PID=$(ps --no-heading $pid | wc -l)
#done
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
cnt=0
#while (("$C_PID" == "0"))
while [ -z "$PID" ]
do
if (($cnt==10))
then
echo "${TAB2}$cnt秒内未启动,请检查!"
break
fi
cnt=$(($cnt+1))
sleep 1s
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
#do
# C_PID=$(ps --no-heading $pid | wc -l)
#done
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
done
okcnt=$(($okcnt+1))
echo "${TAB2}己经成功启动,PID=$PID"
#出借模块和总装模块启动较慢
if [ "$APPNAME" == "deal" ] ;
then
echo "${TAB2}启动中..."
sleep 30s
fi
if [ "$APPNAME" == "dispatch" ] ;
then
echo "${TAB2}启动中..."
sleep 10s
fi
fi
#done
fi
done
if (($cmd2ok==0))
then
echo "command2: all ${APPS[*]}"
else
echo "---------------------------本次启动:$okcnt个服务"
fi
}
stop(){
local APPNAME=
local CLASSNAME=
local cmd2="$1"
local cmd2ok=0
#local C_PID="0"
local okcnt=0
echo "---------------------------开始停止服务..."
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME="${APPS[$i]}.jar"
if [ "$cmd2" == "all" ] || [ $(echo "$cmd2" | tr '[A-Z]' '[a-z]') == $(echo "$APPNAME" | tr '[A-Z]' '[a-z]') ]; then
echo "$APPNAME---$NAME:"
cmd2ok=1
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read PID
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
#do
#C_PID=$(ps --no-heading $PID | wc -l)
#if [ "$C_PID" == "1" ]; then
if [ -n "$PID" ]
then
echo "${TAB2}准备结束,PID=$PID"
kill $PID
#C_PID=$(ps --no-heading $PID | wc -l)
#while (("$C_PID" == "1"))
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
while [ -n "$PID" ]
do
sleep 1s
#C_PID=$(ps --no-heading $PID | wc -l)
PID=`ps -ef |grep "$(echo ${COMMANDPREFIX}${CLASSNAME} |awk -F/ '{print $NF}')" | grep -v grep | awk '{print $2}'`
done
echo "${TAB2}成功结束"
okcnt=$(($okcnt+1))
else
echo "${TAB2}未运行"
fi
#done
fi
done
if (($cmd2ok==0))
then
echo "command2: all ${APPS[*]}"
else
echo "---------------------------本次共停止:$okcnt个服务"
fi
}
case "$1" in
start)
start "$2" "$3"
exit 1
;;
stop)
stop "$2"
;;
restart)
stop "$2"
start "$2"
;;
*)
echo "command1: start|stop|restart"
exit 1
;;
esac