• 适用于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"
    

      

  • 相关阅读:
    java方式实现堆排序
    java方式实现归并排序
    用java方式实现快速排序
    Linux中crontab定时任务
    TCP/IP网络协议初识
    github设置添加ssh
    IDM下载工具使用
    Java程序在内存中运行详解
    GitHub的高级搜索方式
    深入理解JavaScript中的堆与栈 、浅拷贝与深拷贝
  • 原文地址:https://www.cnblogs.com/milton/p/12498881.html
Copyright © 2020-2023  润新知