当我们在linux系统里某个路径下创建了许多个jar包的文件夹后,我们可以通过 java -jar 命令脚本可以快速的启动我们的jar包以及实现全部启动及全部结束jar进程。
/opt/services/providers路径下是各个jar包存放的文件夹,同时将service.sh文件放入在该路径下,通过启动命令:
./service.sh start channeldemand 即可启动 YL-B-Channel-Demand-Provider.jar 包
channeldemand是 YL-B-Channel-Demand-Provider.jar在service.sh中配置的别名
如果需要启动该目录下的所有jar包,通过命令即可启动所有jar包:
./service.sh start all
只需要更改1.程序代码数组:每个jar包起别名 2.程序名称数组:具体每个jar包具体的名称 3.jar包数组:具体的jar包 4.jar包路径数组:jar包所在的路径地址。5.PROJECTDIR属性:具体的jar包文件夹所在路径,/opt/services/providers/KaTeX parse error: Expected '}', got 'EOF' at end of input: {PATHS[i]}。
修改这5个地方即可实现jar包配置。
#!/bin/bash
#####################################################Environment Setting#######################################################
#程序代码数组
APPS=(channelweb channelcorp channeldemand channelresume channeluser omsweb omscode omscorp omsdemand omsuser omszull oss publicweb search)
#程序名称数组
NAMES=(YL-B-Channel-WEB YL-B-Channel-Corp-Provider YL-B-Channel-Demand-Provider YL-B-Channel-Resume-Provider YL-B-Channel-User-Provider YL-B-BOS-WEB YL-B-Code-Provider YL-B-Corp-Provider YL-B-Demand-Provider YL-B-User-Provider YL-B-Zull YL-B-OSS-Provider YL-B-Public-WEB YL-B-Search-Provider)
#jar包数组
JARS=(YL-B-Channel-WEB.jar YL-B-Channel-Corp-Provider.jar YL-B-Channel-Demand-Provider.jar YL-B-Channel-Resume-Provider.jar YL-B-Channel-User-Provider.jar YL-B-BOS-WEB.jar YL-B-Code-Provider.jar YL-B-Corp-Provider.jar YL-B-Demand-Provider.jar YL-B-User-Provider.jar YL-B-Zull.jar YL-B-OSS-Provider.jar YL-B-Public-WEB.jar YL-B-Search-Provider.jar)
#jar包路径数组
PATHS=(BM-B-Channel-WEB BM-B-Channel-Corp-Provider BM-B-Channel-Demand-Provider BM-B-Channel-Resume-Provider BM-B-Channel-User-Provider BM-B-BOS-WEB BM-B-Code-Provider BM-B-Corp-Provider BM-B-Demand-Provider BM-B-User-Provider BM-B-Zull BM-B-OSS BM-B-Public-WEB BM-B-Search-Provider)
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 PATH_LOG=
#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=/opt/services/providers/${PATHS[$i]}
PATH_LOG=/opt/logs/$NAME
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
-XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=256m \
-Xms512m \
-Xmx512m \
-Xmn256m \
-Xss256k \
-XX:SurvivorRatio=8 \
-XX:+UseConcMarkSweepGC \
$CLASSNAME"
exec $command >> $PROJECTDIR/nohup.out 2>&1 &
#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|channelweb|channelcorp|channeldemand|channelresume|channeluser|omsweb|omscode|omscorp|omsdemand|omsuser|omszull"
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=/opt/services/providers/${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|channelweb|channelcorp|channeldemand|channelresume|channeluser|omsweb|omscode|omscorp|omsdemand|omsuser|omszull"
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
进入到jar包所在的文件夹下通过
tail -fn 200 nohup.out 可以查看相关jar包的启动日志。
java -jar -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Xms512m -Xmx512m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC YL-B-Channel-Demand-Provider.jar &