Linux shell脚本,按顺序批量启动多个jar包,批量启动spring cloud的jar包

Posted 漂流的老妖怪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux shell脚本,按顺序批量启动多个jar包,批量启动spring cloud的jar包相关的知识,希望对你有一定的参考价值。

Linux shell脚本,按顺序批量启动多个jar包,批量启动spring cloud的jar包

一. 手动一个一个启动的方式:

nohup java -jar eurekaserver.jar > ../logs/eurekaserver.log 2>&1 &
nohup java -jar configserver.jar > ../logs/configserver.log 2>&1 &
nohup java -jar zuulgatewayserver.jar > ../logs/zuulgatewayserver.log 2>&1 &
nohup java -jar authorizeserver.jar > ../logs/authorizeserver.log 2>&1 &
nohup java -jar logstatisticsserver.jar > ../logs/logstatisticsserver.log 2>&1 &
nohup java -jar appmgrserver.jar > ../logs/appmgrserver.log 2>&1 &
nohup java -jar organizeuserserver.jar > ../logs/organizeuserserver.log 2>&1 &
nohup java -jar jtmopserver.jar > ../logs/jtmopserver.log 2>&1 &

 

二. shell脚本代码:

#!/bin/sh
export EUREKA=eurekaserver.jar
export CONFIG=configserver.jar
export GATEWAY=zuulgatewayserver.jar
export AUTH=authorizeserver.jar
export LOG=logstatisticsserver.jar
export APPMGR=appmgrserver.jar
export ORGANIZEUSER=organizeuserserver.jar
export JTMOP=jtmopserver.jar

export EUREKA_log=../logs/eurekaserver.log
export CONFIG_log=../logs/configserver.log
export GATEWAY_log=../logs/zuulgatewayserver.log
export AUTH_log=../logs/authorizeserver.log
export LOG_log=../logs/logstatisticsserver.log
export APPMGR_log=../logs/appmgrserver.log
export ORGANIZEUSER_log=../logs/organizeuserserver.log
export JTMOP_log=../logs/jtmopserver.log

export EUREKA_port=10080
export CONFIG_port=10085
export GATEWAY_port=10084
export AUTH_port=10088
export LOG_port=10093
export APPMGR_port=10092
export ORGANIZEUSER_port=10087
export JTMOP_port=10091

case "$1" in

start)
        ## 启动eureka
        echo "--------eureka 开始启动--------------"
        nohup java -jar $EUREKA > $EUREKA_log 2>&1 &
        EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$EUREKA_pid" ]
            do
              EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "EUREKA pid is $EUREKA_pid"
        sleep 10
        echo "--------eureka 启动成功--------------"
 
        ## 启动config
        echo "--------开始启动CONFIG---------------"
        nohup java -jar $CONFIG > $CONFIG_log 2>&1 &
        CONFIG_pid=`lsof -i:$CONFIG_port|grep "LISTEN"|awk \'{print $2}\'` 
        until [ -n "$CONFIG_pid" ]
            do
              CONFIG_pid=`lsof -i:$CONFIG_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "CONFIG pid is $CONFIG_pid"
        sleep 30
        echo "---------CONFIG 启动成功-----------"
 
        ## 启动gateway
        echo "--------开始启动GATEWAY---------------"
        nohup java -jar $GATEWAY > $GATEWAY_log 2>&1 &
        GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$GATEWAY_pid" ]
            do
              GATEWAY_pid=`lsof -i:$GATEWAY_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "GATEWAY pid is $GATEWAY_pid"    
        echo "---------GATEWAY 启动成功-----------"
 
        ## 启动auth
        echo "--------开始启动AUTH---------------"
        nohup java -jar $AUTH > $AUTH_log 2>&1 &
        AUTH_pid=`lsof -i:$AUTH_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$AUTH_pid" ]
            do
              AUTH_pid=`lsof -i:$AUTH_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "AUTH pid is $AUTH_pid"     
        echo "---------AUTH 启动成功-----------"
                 
        ## 启动log
        echo "--------开始启动LOG---------------"
        nohup java -jar $LOG > $LOG_log 2>&1 &
        LOG_pid=`lsof -i:$LOG_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$LOG_pid" ]
            do
              LOG_pid=`lsof -i:$LOG_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "LOG pid is $LOG_pid"     
        echo "---------LOG 启动成功-----------"
        
        ## 启动appmgr
        echo "--------开始启动APPMGR---------------"
        nohup java -jar $APPMGR > $APPMGR_log 2>&1 &
        APPMGR_pid=`lsof -i:$APPMGR_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$APPMGR_pid" ]
            do
              APPMGR_pid=`lsof -i:$APPMGR_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "APPMGR pid is $APPMGR_pid"     
        echo "---------APPMGR 启动成功-----------"
        
        ## 启动organizeuser
        echo "--------开始启动ORGANIZEUSER---------------"
        nohup java -jar $ORGANIZEUSER > $ORGANIZEUSER_log 2>&1 &
        ORGANIZEUSER_pid=`lsof -i:$ORGANIZEUSER_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$ORGANIZEUSER_pid" ]
            do
              ORGANIZEUSER_pid=`lsof -i:$ORGANIZEUSER_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "ORGANIZEUSER pid is $ORGANIZEUSER_pid"     
        echo "---------ORGANIZEUSER 启动成功-----------"
        
        ## 启动jtmop
        echo "--------开始启动JTMOP---------------"
        nohup java -jar $JTMOP > $JTMOP_log 2>&1 &
        JTMOP_pid=`lsof -i:$JTMOP_port|grep "LISTEN"|awk \'{print $2}\'`
        until [ -n "$JTMOP_pid" ]
            do
              JTMOP_pid=`lsof -i:$JTMOP_port|grep "LISTEN"|awk \'{print $2}\'`  
            done
        echo "JTMOP pid is $JTMOP_pid"     
        echo "---------JTMOP 启动成功-----------"
        
        echo "===startAll success==="
        ;;

 stop)
        P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===EUREKA process not exists or stop success"
        else
            kill -9 $P_ID
            echo "EUREKA killed success"
        fi
        
        P_ID=`ps -ef | grep -w $CONFIG | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===CONFIG process not exists or stop success"
        else
            kill -9 $P_ID
            echo "CONFIG killed success"
        fi
        
         P_ID=`ps -ef | grep -w $GATEWAY | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===GATEWAY process not exists or stop success"
        else
            kill -9 $P_ID
            echo "GATEWAY killed success"
        fi
        
         P_ID=`ps -ef | grep -w $AUTH | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===AUTH process not exists or stop success"
        else
            kill -9 $P_ID
            echo "AUTH killed success"
        fi
        
         P_ID=`ps -ef | grep -w $LOG | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===LOG process not exists or stop success"
        else
            kill -9 $P_ID
            echo "LOG killed success"
        fi
        
         P_ID=`ps -ef | grep -w $APPMGR | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===APPMGR process not exists or stop success"
        else
            kill -9 $P_ID
            echo "APPMGR killed success"
        fi
        
         P_ID=`ps -ef | grep -w $ORGANIZEUSER | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===ORGANIZEUSER process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ORGANIZEUSER killed success"
        fi
        
         P_ID=`ps -ef | grep -w $JTMOP | grep -v "grep" | awk \'{print $2}\'`
        if [ "$P_ID" == "" ]; then
            echo "===JTMOP process not exists or stop success"
        else
            kill -9 $P_ID
            echo "JTMOP killed success"
        fi
 
        echo "===stop success==="
        ;;   
 
restart)
        $0 stop
        sleep 10
        $0 start
        echo "===restart success==="
        ;;   
esac    
exit 0

 

三. shell脚本批量启动、批量停止、批量重启等步骤: 

  1. 脚本名:startupAll.sh

  2. 先赋予脚本可执行权限:

chmod +x startupAll.sh

 3. 执行批量启动的shell脚本命令:

./startupAll.sh start

 4. 执行批量停止的shell脚本命令:

./startupAll.sh stop

 5. 执行批量重启的shell脚本命令:

./startupAll.sh restart  

 

四. 可能出现如下报错:

  -bash: ./xxx.sh: /bin/sh^M: bad interpreter: No such file or directory

  错误介绍:

   通过上面我们可以看到有个^M个标志,这个标志是:Windows环境下dos格式文件传输到unix系统时,会在每行的结尾多一个^M,所以在执行的时候出现了这种现象,但是你在unix或者Linux环境下使用vi编辑的时候,会在下面显示此文件的格式,比如” startupAll.sh” [dos] 2L,20C字样,表示这是一个【dos】的格式文件,如果是MAC的系统则会出现【MAC】的字样,因为文件格式的原因,有时候我们是unix程序或shell程序,则就要把dos文件转化为unix的文件格式。

  错误解决方式:有如下四种解决方式

    解决方式1:

      也是最简单的方法,直接在当前的文件目录下执行下面命令

sed -i -e \'s/\\r$//\' startupAll.sh

    解决方式2:

      使用unix/Linux中的vi命令,更改fileformat格式

      首先执行:

vi startupAll.sh

      使用vi startupAll.sh查看时在最后一行会出现了下面的

"startupAll.sh" [dos] 2L, 19C

      说明这个文件编码方式是windows编辑的,必须转化格式为unix格式 ,按ESC键,再次执行下面命令后

:set ff

      敲回车,出现如下

fileformat=dos

      修改为下面

vi test.sh
:set ff=unix
:x

    解决方式3:

      使用dos2unix和unix2dos命令

        Linux提供了两种文本格式相互转化的命令:dos2unix和unix2dos,dos2unix把”\\r\\n”转化成”\\n”,unixtodos把”\\n”转化成”\\r\\n”。Java程序中,使用 Runtime.getRuntime().exec(“unix2dos “+ fileName);来实现对文本格式互转命令的调用。多说一句,执行指令前要先看看linux上是否有安装unix2dos/dos2unix工具。

        1.首先查看是否安装了这个工具,使用grep正则使用

yum list installed | grep -nE  \'dos2unix|dos2\'

         如果安装了这个工具,则执行下面命令

dos2unix  startupAll.sh

        出现下面显示说明转化ok

[root@hostname webapps]# dos2unix j.sh
dos2unix: converting file j.sh to Unix format ...
[root@hostname webapps]#

    解决方式4:

      在编辑代码的时候设置编辑器的line endings编码方式

      如果你在Windows或Mac上使用sublime文本编辑脚本:单击View > Line ending > Unix并再次保存文件。例如:

      

 

      在notepad++中,您可以通过按下按钮为文件设置它 Edit –> EOL Conversion –> UNIX/OSX Format

      

 

      Eclipse用户,您可以更改文件编码直接从 File > Convert Line Delimiters To > Unix (LF, \\n, 0Α, ¶)

       

 

       或者更改 Unix on Window > Preferences > General > Workspace panel:

       

 

      通过以上4中方式,选择其中一种即可,所以以后要注意这个文件的格式最好把你使用的编辑器设置为unix格式。

 

      

 

以上是关于Linux shell脚本,按顺序批量启动多个jar包,批量启动spring cloud的jar包的主要内容,如果未能解决你的问题,请参考以下文章

shell 一次按顺序执行多个脚本

shell脚本应用

shell实现交互式在多台服务器批量执行命令

Linux批量处理的核心-shell脚本

shell脚本同时启动关闭多个tomcat

Linux下批量启动jar包的shell脚本