shell脚本批量/单独启动、停止、重启java独立jar程序

本人最近半年使用阿里dubbo做开发,并在公司内部大力进行推广,将原来一个笨重且不易于维护的大项目切分成多个相对独立的java程序,好处是显而易见的,但是随着切分的独立运行程序包越来越多,程序的部署变成了一件非常头痛的问题,无耐之下,本人想到可否写一个shell脚本来批量/单独启动、停止、重启这些独立的java程序,之前没有写过shell脚本,研究二天后,终于将这个脚本写出来了,以后部署起来方便多了,废话不多说,直接贴上shell脚本,有需要的朋友可以根据自己项目修改前面的程序代码数组、程序名称数组、jar包数组、jar包路径数组就行了。

 

service.sh

 

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

#程序代码数组
APPS=(pushcode thirdpayment security redis commservice wechat point useraccount coupon interest experience dealaccount user send payment sms deal dispatch dealload recv innermsg reward finacial debt pushmsg esign crmda)

#程序名称数组

NAMES=(邀请码模块 第三方支付模块 安全模块 Redis模块 公共服务模块 微信服务模块 积分账户子系统 用户账户子系统 用户模块 队列消息发送模块 支付模块 短信系统 交易系统 消息队列模块 消息队列接收模块 站内信模块 奖励管理模块 结算系统 消息推送模块 电子签章 数据仓库)

#jar包数组

JARS=(pushcode-provider-0.0.1.jar comm-thirdpayment-provider-0.0.1.jar comm-security-provider.jar comm-redis-provider-0.0.1.jar comm-service-provider-0.0.1.jar action-provider-wechat-0.0.1.jar action-provider-point-0.0.1.jar action-provider-useraccount-0.0.1.jar action-provider-user-0.0.1.jar comm-message-send-provider-0.0.1.jar action-provider-payment-0.0.1.jar action-provider-sms-0.0.1.jar action-provider-deal-0.0.1.jar message-recv-dealload-0.0.1.jar comm-message-recv-provider-0.0.1.jar action-provider-innermsg-0.0.1.jar action-provider-reward-0.0.1.jar action-provider-finacial-0.0.1.jar message-pushmsg-0.0.1.jar provider-esign-0.0.1.jar provider-crmda-0.0.1.jar)

#jar包路径数组

PATHS=(/application/provider/pushcode /application/provider/thirdpayment /application/provider/security /application/provider/redis /application/provider/commservice /application/provider/wechat /application/provider/point /application/provider/useraccount  /application/provider/user /application/provider/message/send /application/provider/payment /application/provider/sms /application/provider/deal  /application/provider/message/dealload /application/provider/message/recv /application/provider/innermsg /application/provider/reward /application/provider/finacial /application/provider/pushmsg /application/provider/esign /application/provider/crmda)

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程序的代码即可

posted @ 2016-08-06 21:47  阿乐子  阅读(9843)  评论(0编辑  收藏  举报