美文网首页
spring boot 项目启动脚本 run.sh

spring boot 项目启动脚本 run.sh

作者: hemiao3000 | 来源:发表于2021-12-16 15:46 被阅读0次

    各个微服务的 run.sh

    #!/bin/bash
    
    # 程序名。此处要改
    PRO_NAME=address-service
    
    pp=$(pwd)  
    
    if [ "$2" != "" ]; then
      port=$2
    fi
    
    exit_if_app_is_running() {
      PID=`ps -ef | grep "${PRO_NAME}.jar" | grep -v "$0" | grep -v "grep" | grep -v stop | awk '{print $2}'`
      if [ "$PID" == "" ]; then
        echo "$PRO_NAME is stopped."  
        exit 0
      fi
      for id in $PID
      do
        echo "$PRO_NAME($id) is running..."  
      done
      
      exit 0
    }
    
    exit_if_app_is_stopped() {
      PID=`ps -ef | grep "${PRO_NAME}.jar" | grep -v "$0" | grep -v "grep" | grep -v stop | awk '{print $2}'`
      if [ "$PID" == "" ]; then
        echo "$PRO_NAME is stopped."  
        exit 0
      fi
    }
    
    exit_if_port_in_use() {
      PID=`lsof -i:$port | grep "LISTEN" | awk '{print $2}'`
      if [ "$PID" != "" ]; then
        echo "port($port) is in use"
        exit 0
      fi
    }
    
    # 启动方法
    start() {
      # 启动程序
      echo "$(date "+%Y-%m-%d %H:%M:%S") $PRO_NAME($port) starting ..." 
      nohup java -Xms256m -Xmx256m -jar ${pp}/target/${PRO_NAME}.jar  --server.port=${port} 1>>./out.log 2>&1 &
    
      # 查询日志检测java程序是否启动成功
      PID=`lsof -i:$port | grep "LISTEN" | awk '{print $2}'`
      until [ -n "$PID" ]
      do
        PID=`lsof -i:$port | grep "LISTEN" | awk '{print $2}'`
        echo "$(date "+%Y-%m-%d %H:%M:%S") $PRO_NAME($port) starting ..." 
        sleep 5s
      done
    
      echo "$(date "+%Y-%m-%d %H:%M:%S") $PRO_NAME($PID:$port) started."  
    }
    
    
    # 停止方法
    stop() {
      PID=`ps -ef | grep "${PRO_NAME}.jar" | grep -v "$0" | grep -v "grep" | grep -v stop | awk '{print $2}'`
    
      for id in $PID
      do
        kill -9 $id
        echo "$(date "+%Y-%m-%d %H:%M:%S") $PRO_NAME($id) stopped."  
      done
    }
    
    
    case "$1" in
      status)
        exit_if_app_is_running
        ;;
    
      start)
        if [ "$2" == "" ]; then
          echo "Userage: $0 start <port>"
          exit 1
        fi
    
        exit_if_port_in_use
    
        start
        ;;
      stop)
        exit_if_app_is_stopped
        stop
        ;;
      restart)
        if [ "$2" == "" ]; then
          echo "Userage: $0 restart <port>"
          exit 1
        fi
    
        stop
        start
        ;;
      *)
        echo "Userage: $0 {start <port>|stop|restart <port>}"
        exit 1
    esac
    

    聚合项目的 run.sh

    #!/bin/bash
    
    # 各个微服务目录名(不含网关)
    arr=(
    address-service
    community-service
    complaint-form-service
    handin-house-service
    house-rent-service
    login-service
    party_member
    party_organization
    property-service
    self-apply-service
    yu-building-service
    yu-house-service
    yu-owner-service
    # gateway
    )
    
    # echo ${#arr[*]}
    # echo ${arr[0]}
    # echo ${arr[12]}
    # for ((i=0; i<${#arr[*]}; i++))
    # do
    #  echo ${arr[$i]} 
    #  echo $[8080+i]
    # done
    
    
    find ./ -type f -name 'run.sh' -exec chmod a+x {} \;
    
    status() {
      # 网关
      cd gateway && ./run.sh status && cd ..
      # 其它微服务
        for ((i=0; i<${#arr[*]}; i++))
        do
        cd ${arr[$i]} && ./run.sh status && cd ..
        done
    }
    
    stop () {
      # 网关
      cd gateway && ./run.sh stop && cd ..
      # 其它微服务
        for ((i=0; i<${#arr[*]}; i++))
        do
        cd ${arr[$i]} && ./run.sh stop && cd ..
        done
    }
    
    start() {
      # 其它微服务
        for ((i=0; i<${#arr[*]}; i++))
        do
        cd ${arr[$i]} && ./run.sh start $[8081+i] && cd ..
        done
    
      # 网关
      cd gateway && ./run.sh start 8080 && cd ..
    }
    
    case "$1" in
      status)
        status
        ;;
      start)
        start
        ;;
      stop)
        stop
        ;;
      restart)
        stop
        start
        ;;
      *)
        echo "Userage: $0 {start|stop|restart}"
        exit 1
    esac
    

    相关文章

      网友评论

          本文标题:spring boot 项目启动脚本 run.sh

          本文链接:https://www.haomeiwen.com/subject/yvsvfrtx.html