Shell script Batch/standalone start, stop, restart Java standalone Jar program

Source: Internet
Author: User



I recently half a year using Ali Dubbo to do development, and in the company vigorously promote, the original a bulky and not easy to maintain the large project into a number of relatively independent Java programs, the benefits are obvious, but with the split of the independent running packages more and more, The deployment of the program has become a very headache, without resistance, I think can write a shell script in bulk/separate start, stop, restart these independent Java programs, not previously written shell scripts, the study two days later, finally the script is written out, and later deployed more convenient, Needless to say, directly affixed to the shell script, the need for friends can modify the previous program code array, program name array, jar package array, jar package path array according to your project.






service.sh






#!/bin/bash
#################################################### #Environment setting####################################### ################



#Program code array
apps= (pushcode thirdpayment security Redis commservice WeChat point UserAccount coupon interest Experience Dealaccount use R Send payment SMS deal dispatch dealload recv innermsg reward finacial debt pushmsg esign crmda)



#Program name array



names= (Invite code module third party payment Module security Module Redis Module Public Service Module Service Module Integration account Subsystem user account subsystem user module queue message send module payment module SMS system trading system Message Queuing module Message Queuing receive module station Inner letter module Reward Management module settlement System Message Push module E-SEAL Data Warehouse)



#Jar package array



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 package path array



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= "$"
Local cmd2ok=0
Local cnt=0
Local okcnt=0
#local c_pid= "0"
#for i in ' seq 0 22 '
echo "---------------------------Start service ..."
For ((i=0;i<${#APPS [@]};i++))
Do
appname=${apps[$i]}
name=${names[$i]}
classname=${jars[$i]}
projectdir=${paths[$i]}
If ["$cmd 2" = = "All"] | | ["$cmd 2" = = "$APPNAME"]; Then
Cmd2ok=1
C_pid= "0"
Cnt=0
#ps-ef | grep "$CLASSNAME" | awk ' {print $} ' | While read PID
Pid= ' Ps-ef |grep $ (echo $CLASSNAME |awk-f/' {print $NF} ') | Grep-v grep | awk ' {print $} '
#do
#C_PID =$ (ps--no-heading $pid | wc-l)
#if ["$C _pid" = = "1"]; Then
If [-N "$PID"]
Then
echo "$APPNAME---$NAME: Run, 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 $} ' | 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 $} '
Cnt=0
#while (("$C _pid" = = "0"))
While [-Z "$PID"]
Do
if (($cnt ==30))
Then
echo "$APPNAME---$NAME: not started in $cnt seconds, please check! "
Break
Fi
cnt=$ (($cnt + 1))
Sleep 1s
#ps-ef | grep "$CLASSNAME" | awk ' {print $} ' | 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 $} '
Done
okcnt=$ (($okcnt + 1))
echo "$APPNAME---$NAME: Successfully started, pid= $PID"
#出借模块和总装模块启动较慢
If ["$APPNAME" = = "Deal"];
Then
echo "$APPNAME---$NAME: Start ..."
Sleep 30s
Fi
If ["$APPNAME" = = "dispatch"];
Then
echo "$APPNAME---$NAME: Start ..."
Sleep 10s
Fi
Fi
#done
Fi
Done
if (($cmd 2ok==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 "---------------------------This launch: $okcnt services"
Fi
}

Stop () {
Local appname=
Local classname=
Local projectdir=
Local command= "sh service.sh stop"
Local cmd2= "$"
Local cmd2ok=0
#local c_pid= "0"
Local okcnt=0
echo "---------------------------Start stop service ..."
For ((i=0;i<${#APPS [@]};i++))
Do
appname=${apps[$i]}
name=${names[$i]}
classname=${jars[$i]}
projectdir=${paths[$i]}
If ["$cmd 2" = = "All"] | | ["$cmd 2" = = "$APPNAME"]; Then
Cmd2ok=1
#ps-ef | grep "$CLASSNAME" | awk ' {print $} ' | While Read PID
Pid= ' Ps-ef |grep $ (echo $CLASSNAME |awk-f/' {print $NF} ') | Grep-v grep | awk ' {print $} '
#do
#C_PID =$ (ps--no-heading $PID | wc-l)
#if ["$C _pid" = = "1"]; Then
If [-N "$PID"]
Then
echo "$NAME:P id= $PID ready to finish."
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 $} '
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 $} '
Done
echo "$NAME: Successful End"
okcnt=$ (($okcnt + 1))
Else
echo "$NAME: Not Running"
Fi
#done
Fi
Done
if (($cmd 2ok==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 "---------------------------This total stop: $okcnt Services"
Fi
}

Case "$" in
Start
Start "$"
Exit 1
;;
Stop
Stop "$"
;;
Restart
Stop "$"
Start "$"
;;
*)
echo "Command1:start|stop|restart"
Exit 1
;;
Esac





Linux Command usage:



1. Start all jar programs: SH service.sh start all



2. Stop all jar programs: SH service.sh stop all



3. Restart all jar programs: SH service.sh restart all



4. Start, stop, and restart a jar program individually: Replace all of the last faces with the code of a JAR program



Shell script Batch/standalone start, stop, restart Java standalone Jar program


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.