脚本代码server.sh,此脚本具有启动、停止、查看应用状态、重启、更新应用的功能,既可以独立操作也可以批量操作,在这里说明一下,更新jar包应用需自己设置好更新目录和备份目录,以及应用日志目录,脚本更新时会自动备份原应用。
#!/bin/bash
#####################################################Environment Setting#######################################################
sed -i'' "s/\r//" "$0"
#chengxu daima shuzu
APPS=(app-service eth-job fanshang)
#chengxu mingcheng shuzu
NAMES=(app-service eth-job fanshang)
#jar bao shuzu
JARS=(app-service.jar eth-job.jar fanshang.jar)
#jar bao lujing shuzu
PATHS=(/opt/apps/api /opt/apps/api /opt/apps/api)
update_paths="/data/update" 更新目录
backup_paths="/data/backup" 备份目录
FILE_NAME=`date +%y%m%d%H%M`
start() {
local APPNAME=
local NAME=
local CLASSNAME=
local PROJECTDIR=
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`
echo ""
echo "---------------------------bigin start---------------------------"
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME=${JARS[$i]}
PROJECTDIR=${PATHS[$i]}
if [ "$cmd2" == "all" ] || [ "$cmd2" == "$APPNAME" ]; then
cmd2ok=1
C_PID="0"
cnt=0
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
PID=`ps -ef |grep $(echo $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 "$APPNAME---$NAME:already running,PID=$PID"
#okcnt=$(($okcnt+1))
else
cd $PROJECTDIR
#rm -f $PROJECTDIR/nohup.out
command="nohup java -jar $CLASSNAME"
exec $command >> $PROJECTDIR/logs/$APPNAME$FILE_NAME.log 2>&1 &
if [ $? -eq 0 ];then
echo ""
else
echo "There is a false start, please look at the log"
exit 1
fi
sleep 3
#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 $CLASSNAME |awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
cnt=0
#while (("$C_PID" == "0"))
if [ -n "$PID" ]
then
PID=`ps -ef |grep $(echo $CLASSNAME |awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
echo "$APPNAME---$NAME:already running succeed,PID=$PID"
else
while [ -z "$PID" ]
do
if (($cnt==30))
then
echo "$APPNAME---$NAME:$cnt miao not start,please jiancha"
break
fi
cnt=$(($cnt+1))
sleep 1s
done
fi
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
#do
# C_PID=$(ps --no-heading $pid | wc -l)
#done
#
if [ "$APPNAME" == "deal" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 30s
fi
if [ "$APPNAME" == "dispatch" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 10s
fi
fi
#done
okcnt=$(($okcnt+1))
fi
done
if (($cmd2ok == 0))
then
echo "command2: all|app-service|eth-job|fanshang"
else
echo "ben ci start:$okcnt services"
fi
}
stop(){
local APPNAME=
local CLASSNAME=
local PROJECTDIR=
local command="sh service.sh stop"
local cmd2="$1"
local cmd2ok=0
#local C_PID="0"
local okcnt=0
echo ""
echo "---------------------------bigin stop service---------------------------"
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME=${JARS[$i]}
PROJECTDIR=${PATHS[$i]}
if [ "$cmd2" == "all" ] || [ "$cmd2" == "$APPNAME" ]; then
cmd2ok=1
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read PID
PID=`ps -ef |grep $(echo $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 "$NAME:PID=$PID zhunbei over"
kill $PID
#C_PID=$(ps --no-heading $PID | wc -l)
#while (("$C_PID" == "1"))
PID=`ps -ef |grep $(echo $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 $CLASSNAME |awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
done
echo "$NAME:over succeed"
okcnt=$(($okcnt+1))
else
echo "$NAME:not running"
fi
#done
fi
done
if (($cmd2ok==0))
then
echo "command2: all|app-service|eth-job|fanshang"
else
echo "ben ci stop:$okcnt services"
fi
}
status(){
local APPNAME=
local CLASSNAME=
local PROJECTDIR=
local command="sh service.sh status"
local cmd2="$1"
local cmd2ok=0
local okcnt=0
echo ""
echo "---------------------------look status---------------------------"
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME=${JARS[$i]}
PROJECTDIR=${PATHS[$i]}
if [ "$cmd2" == "all" ] || [ "$cmd2" == "$APPNAME" ]; then
cmd2ok=1
C_PID="0"
cnt=0
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
PID=`ps -ef |grep $(echo $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 "$APPNAME---$NAME:already running,PID=$PID"
#okcnt=$(($okcnt+1))
else
echo "$NAME:not running"
#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 $CLASSNAME |awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
cnt=0
#while (("$C_PID" == "0"))
okcnt=$(($okcnt+1))
#
if [ "$APPNAME" == "deal" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 30s
fi
if [ "$APPNAME" == "dispatch" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 10s
fi
fi
#done
fi
done
if (($cmd2ok == 0))
then
echo "command2: all|app-service|eth-job|fanshang"
else
echo "ben ci $okcnt services not running"
fi
}
update(){
local APPNAME=
local CLASSNAME=
local PROJECTDIR=
local command="sh service.sh update"
local cmd2="$1"
local cmd2ok=0
local okcnt=0
FILE_NAME=`date +%y%m%d%H%M`
for(( i=0;i<${#APPS[@]};i++))
do
APPNAME=${APPS[$i]}
NAME=${NAMES[$i]}
CLASSNAME=${JARS[$i]}
PROJECTDIR=${PATHS[$i]}
if [ "$cmd2" == "all" ] || [ "$cmd2" == "$APPNAME" ]; then
cmd2ok=1
C_PID="0"
cnt=0
#ps -ef | grep "$CLASSNAME" | awk '{print $2}' | while read pid
echo ""
echo "---------------------------bigin update service---------------------------"
cd $update_paths
update_name=$(ls | grep $CLASSNAME)
if [ -z "$update_name" ]
then
echo "Please upload the update package to the update directory"
exit 1
else
cd $PROJECTDIR
PROJECTDIR_name=$(ls | grep $CLASSNAME)
if [ -n "$PROJECTDIR_name" ]
then
mv $CLASSNAME $backup_paths/$APPNAME$FILE_NAME.jar
sleep 2
PROJECTDIR_name1=$(ls | grep $CLASSNAME)
while [ -n "$PROJECTDIR_name1" ]
do
sleep 2
done
cd $update_paths
mv $CLASSNAME $PROJECTDIR
update_name2=$(ls | grep $CLASSNAME)
while [ -n "$update_name2" ]
do
sleep 2
done
else
cd $update_paths
mv $CLASSNAME $PROJECTDIR
update_name3=$(ls | grep $CLASSNAME)
while [ -n "$update_name3" ]
do
sleep 2
done
fi
echo "$APPNAME update sucess !"
fi
cnt=0
#while (("$C_PID" == "0"))
okcnt=$(($okcnt+1))
#
if [ "$APPNAME" == "deal" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 30s
fi
if [ "$APPNAME" == "dispatch" ] ;
then
echo "$APPNAME---$NAME:starting.."
sleep 10s
fi
fi
#done
done
if (($cmd2ok == 0))
then
echo "command2: all|app-service|eth-job|fanshang"
else
echo "ben ci have $okcnt services updated"
fi
}
case "$1" in
start)
start "$2"
exit 1
;;
stop)
stop "$2"
;;
status)
status "$2"
;;
restart)
stop "$2"
start "$2"
;;
update)
stop "$2"
update "$2"
start "$2"
;;
*)
echo "command1: start|stop|restart"
exit 1
;;
esac
1,启动所有jar包:sh service.sh start all
2,停止所有jar包:sh service.sh stop all
3,重启所有jar包:sh service.sh restart all
4,查看所有jar包运行状态:sh service.sh status all
5,更新所有jar包并启动:sh service.sh update all 注意:更新前需将更新包上传到更新目录
4,重启或者停止单个jar包,只需要将all替换为相应的模块代码就行