码迷,mamicode.com
首页 > 编程语言 > 详细

shell脚本批量启动jar

时间:2019-04-01 17:01:29      阅读:504      评论:0      收藏:0      [点我收藏+]

标签:shell脚本   tar   ++   environ   print   java   art   $1   rms   

#!/bin/bash
#####################################################Environment Setting#######################################################

#程序代码数组
APPS=(shop euaker config)

#程序名称数组

NAMES=(店铺模块 euaker模块 config模块)

#jar包
JARS=(message-pushmsg-0.0.1.jar provider-esign-0.0.1.jar provider-crmda-0.0.1.jar)

#jar包路径数组

PATHS=(/jjj/shop /jjj/euaker /jjj/config)

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 "---------------------------开始启动服务..."
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:己经运行,PID=$PID"
#okcnt=$(($okcnt+1))
else
cd $PROJECTDIR
rm -f $PROJECTDIR/nohup.out
command="nohup java -jar $CLASSNAME"
exec $command >> $PROJECTDIR/nohup.out &
#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"))
while [ -z "$PID" ]
do
if (($cnt==30))
then
echo "$APPNAME---$NAME:$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 $CLASSNAME |awk -F/ ‘{print $NF}‘) | grep -v grep | awk ‘{print $2}‘`
done
okcnt=$(($okcnt+1))
echo "$APPNAME---$NAME:己经成功启动,PID=$PID"
#出借模块和总装模块启动较慢
if [ "$APPNAME" == "deal" ] ;
then
echo "$APPNAME---$NAME:启动中..."
sleep 30s
fi
if [ "$APPNAME" == "dispatch" ] ;
then
echo "$APPNAME---$NAME:启动中..."
sleep 10s
fi
fi
#done
fi
done
if (($cmd2ok==0))
then
echo "command2: all|pushcode|thirdpayment|security|redis|commservice|wechat|point|useraccount|coupon|interest|experience|dealaccount|user|send|payment|sms|deal|dispatch|dealload|recv|innermsg|reward|finacial|debt"
else
echo "---------------------------本次启动:$okcnt个服务"
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 "---------------------------开始停止服务..."
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准备结束"
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:成功结束"
okcnt=$(($okcnt+1))
else
echo "$NAME:未运行"
fi
#done
fi
done
if (($cmd2ok==0))
then
echo "command2: all|pushcode|thirdpayment|security|redis|commservice|wechat|point|useraccount|coupon|interest|experience|dealaccount|user|send|payment|sms|deal|dispatch|dealload|recv|innermsg|reward|finacial|debt"
else
echo "---------------------------本次共停止:$okcnt个服务"
fi
}

case "$1" in
start)
start "$2"
exit 1
;;
stop)
stop "$2"
;;
restart)
stop "$2"
start "$2"
;;
*)
echo "command1: start|stop|restart"
exit 1
;;
esac

 

 

linux命令用法:
1.启动所有jar程序:sh service.sh start all
2.停止所有jar程序:sh service.sh stop all
3.重启所有jar程序:sh service.sh restart all
4.单独启动、停止、重启某个jar程序:把最后面的all替换为某个jar程序的代码即可

shell脚本批量启动jar

标签:shell脚本   tar   ++   environ   print   java   art   $1   rms   

原文地址:https://www.cnblogs.com/laixin09/p/10637197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!