• 适用于Spring Boot Jar的启停部署脚本


    使用-z或-n对一个变量判空时, 若直接使用[ -n ${ARG} ]这种形式, 当${ARG}中有空格将会报错,

    line 27: [: sd: binary operator expected
    

    需要使用

     [[ -n ${ARG} ]] 或 [ -n "${ARG}" ]
    

    完整脚本

    # Please define the absolute paths
    JAVA_HOME=/opt/java/latest
    RUN_BASE=/home/tomcat/run
    BUILD_FOLDER=/home/tomcat/builds
    ARCHIVE_FOLDER=/home/tomcat/archives
    JAVA_OPTS="-server -Xms512m -Xmx512m -XX:MaxNewSize=256m -Djava.awt.headless=true"
    
    start() {
      echo -n "[$(date +%Y%m%d-%H%M%S)] Start: "
    
      pid=`ps -ef|grep ${JAR_FILE}|grep -v 'grep'|grep 'java'|awk '{print $2}'`
      if [ -z ${pid} ] ; then
        echo $1 "$2"
        nohup ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar ${JAR_FILE} "$2"  >/dev/null 2>&1 &
      else
        echo $1' is running at pid: '${pid}
      fi
    }
    
    stop() {
      if [ -z $1 ]; then
        exit 1
      fi
    
      echo -n "[$(date +%Y%m%d-%H%M%S)] Stop: "
      pid=`ps -ef|grep ${JAR_FILE}|grep -v 'grep'|grep 'java'|awk '{print $2}'`
      if [ -z $pid ] ; then
        echo $1' is not running'
      else
        echo $1' is running at pid:'${pid}
        kill $pid
        sleep 5
      fi
    
      pid=`ps -ef|grep ${JAR_FILE}|grep -v 'grep'|grep 'java'|awk '{print $2}'`
      if [ -z $pid ] ; then
        echo 'Done'
      else
        echo 'Still running, now killing with pid:'${pid}
        ps -ef|grep ${JAR_FILE}|grep -v 'grep'|grep 'java'|awk '{print $2}'|xargs kill -s 9
      fi
    }
    
    restart() {
      stop $1
      start $1 "$2"
    }
    
    deploy() {
      # -f filename - Check for regular file existence not a directory
      [ -f ${BUILD_FOLDER}/$1 ] || exit 1
    
      stop $1
      df -h
      echo ''
    
      local BACKUP_FILENAME=$1.$(date +%Y%m%d-%H%M)
      echo -n "[$(date +%Y%m%d-%H%M%S)] Deploy: "
      echo -n "backup ${BACKUP_FILENAME}... "
      mv ${JAR_FILE} ${ARCHIVE_FOLDER}/${BACKUP_FILENAME}
      echo -n "copy... "
      cp -f ${BUILD_FOLDER}/$1 ${RUN_BASE}/
      echo 'done'
    
      start $1 "$2"
    }
    
    # -z tests for a null string
    if [ -z $1 ]; then
      echo $"Usage: $0 {start|stop|restart|deploy} [some.jar] [options]
    "
      echo $"  - option: '--server.port=8080 --dubbo.protocol.port=8081'"
      exit 2
    elif [ -z $2 ]; then
      echo $"Usage: $0 $1 [some.jar] [options]"
      exit 2
    fi
    
    JAR_FILE=${RUN_BASE}/$2
    # -e Check for file existence regardless of type (node, directory, socket)
    if [ ! -e ${JAR_FILE} ]; then
      echo "File not exists: $JAR_FILE"
      if [ ! -e ${BUILD_FOLDER}/$2 ]; then
        echo "File not exists: ${BUILD_FOLDER}/$2"
        exit 1
      fi
      echo "Copy from ${BUILD_FOLDER}"
      cp -f ${BUILD_FOLDER}/$2 ${RUN_BASE}/
    fi
    
    case "$1" in
        start)
            if [ -z "$3" ]; then
              echo $"Usage: $0 $1 $2 [options]"
              exit 2
            fi
            $1 $2 "$3"
            ;;
        stop)
            $1 $2
            ;;
        restart)
            if [ -z "$3" ]; then
              echo $"Usage: $0 $1 $2 [options]"
              exit 2
            fi
            $1 $2 "$3"
            ;;
        deploy)
            if [ -z "$3" ]; then
              echo $"Usage: $0 $1 $2 [options]"
              exit 2
            fi
            $1 $2 "$3"
            ;;
        *)
            echo $"Usage: $0 {start|stop|restart|deploy} $2 [options]"
            exit 2
    esac
    

      

    使用示例

    tc.sh deploy procyon-commons.jar "--server.port=8084 --dubbo.protocol.port=8085 --procyon.vm_id=01"
    

      

  • 相关阅读:
    Android之JSON格式数据解析
    SSH面试题锦集
    Mysql
    (二)Java基础巩固
    (一)Java基础巩固
    (五)Oracle函数 序列 约束 索引
    让css初学者抓狂的属性float
    微信小程序(4)--二维码窗口
    微信小程序(3)--页面跳转和提示框
    微信小程序(2)--下拉刷新和上拉加载更多
  • 原文地址:https://www.cnblogs.com/milton/p/12498881.html
Copyright © 2020-2023  润新知