用于Spring Boot Jar部署的shell脚本

使用getopts改写的脚本

# Please define the absolute path of tomcat instance
JAVA_HOME=/opt/jdk/latest
BASE_DIR=/home/tomcat
JAVA_OPTS="-server -Xms512m -Xmx512m -XX:MaxNewSize=256m -Djava.awt.headless=true"
EXTRA_OPTS=""
TC_ERROR=0


start() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] Start: "

  pid=`ps -ef|grep $1|grep -v 'grep'|grep 'java'|awk '{print $2}'`
  if [ -z ${pid} ] ; then
    if [ ! -e ${JAVA_HOME}/bin/java ]; then
      echo ${JAVA_HOME} ' is not a valid JAVA_HOME path'
      exit 2
    fi
    echo JAVA_HOME:${JAVA_HOME}, JAR:$1, port:$2
    nohup ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar $1 --server.port=$2 ${EXTRA_OPTS} >/dev/null 2>&1 &
  else
    echo $1' is running at pid: '${pid}
  fi
}


stop() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] Stop: "
  pid=`ps -ef|grep $1|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 $1|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 $1|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_DIR}/$JAR_FILE ] || exit 1

  stop $1
  df -h
  echo ''

  echo -n "[$(date +%Y%m%d-%H%M%S)] Deploy: "
  if [ -e $1 ]; then
    echo -n "remove $1... "
    rm $1
  fi

  echo -n "copy... "
  cp -f ${BUILD_DIR}/$JAR_FILE ${RUN_DIR}/$JAR_FILE
  echo 'done'

  start $1 $2
}

help_func() {
  echo "Usage:"
  echo "   $0 [-d BASE_DIR] [-j JAVA_HOME] [-f JAR_FILE] [-p PORT] [-m ACT_MODE] [-v VM_OPTION]"
  echo "Description:"
  echo "-d Path of the workspace, optional, default: $BASE_DIR"
  echo "-f The jar file name, e.g. my-project.jar"
  echo "-j Path to JDK home, optional, default: $JAVA_HOME"
  echo "-m Action mode, e.g. start, restart, stop, etc."
  echo "-p Service port, e.g. 8080"
  echo "-e extra jar arguments, e.g. --asp.root_path=/path"
  echo "-v JAVA_OPTS, optional, default: $JAVA_OPTS"
  echo ""
  echo "Examples:"
  echo "   $0 -f asp-commons.jar -p 8761 -m restart"
  exit 0
}

# 如果字符后面没有冒号, 说明该字符的选项不需要值, 如 -i
# 如果字符后面跟着一个冒号, 表明该字符的选项需要一个值, 其参数和值之间需要空格分隔, 如 -j aaa.jar, 冒号和问号不能被用作选项字符
# getopts每次被调用时, 会将下一个选项字符放置到变量中, 通过OPTARG可以取到参数值
# 如果option前面加冒号, 代表忽略错误
while getopts 'd:e:j:f:m:p:v:h' OPT; do
  case $OPT in
    d) BASE_DIR="$OPTARG";;
    f) JAR_FILE="$OPTARG";;
    j) JAVA_HOME="$OPTARG";;
    m) ACT_MODE="$OPTARG";;
    p) PORT="$OPTARG";;
    v) JAVA_OPTS="$OPTARG";;
    e) EXTRA_OPTS="$OPTARG";;
    h) help_func $0;;
    ?) help_func $0;;
    esac
done

shift $(($OPTIND - 1))

# Print helpFunction in case parameters are empty

if [ -z "$ACT_MODE" ]
then
   echo "Parameter -m is missing";
   TC_ERROR=1
fi

if [ -z "$JAR_FILE" ]
then
   echo "Parameter -f is missing";
   TC_ERROR=1
fi

if [ $TC_ERROR -gt 0 ]
then
   help_func
fi

RUN_DIR=${BASE_DIR}/run
BUILD_DIR=${BASE_DIR}/builds
ARCHIVE_DIR=${BASE_DIR}/archives
TMP_DIR=${BASE_DIR}/tmp

# -e Check for file existence regardless of type (node, directory, socket)
#if [ ! -e ${RUN_DIR}/${JAR_FILE} ]; then
#  echo ${RUN_DIR}/${JAR_FILE} 'does not exist'
#  exit 2
#fi

case "$ACT_MODE" in
  start)
      # -e Check for file existence regardless of type (node, directory, socket)
      if [ ! -e ${RUN_DIR}/${JAR_FILE} ]; then
        echo ${RUN_DIR}/${JAR_FILE} 'does not exist'
        exit 2
      fi
      if [ -z "$PORT" ]
      then
         echo "Parameter -p is missing";
         exit 2
      fi
      start ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  stop)
      stop ${RUN_DIR}/${JAR_FILE}
      ;;
  restart)
      restart ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  deploy)
      deploy ${RUN_DIR}/${JAR_FILE} $PORT
      ;;
  *)
      echo "Unknown action mode"
      help_func
esac

旧版

用于在Jenkins将jar发送到目标节点之后的部署操作, 包含deploy, start, stop, restart功能. 在deploy时会自动备份原jar至指定目录

# Please define the absolute path of tomcat instance
JAVA_HOME=/opt/java/jdk1.8.0_101
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: "
 
  case "$1" in
    some-common.jar)
        JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:MaxNewSize=512m -Djava.awt.headless=true"
        echo "Use Xms1024m JAVA_OPTS"
        ;;
    *)
        echo "Use default JAVA_OPTS"
  esac
 
  pid=`ps -ef|grep ${JAR_FILE}|grep -v 'grep'|grep 'java'|awk '{print $2}'`
  if [ -z ${pid} ] ; then
    echo $1 port:$2
    nohup ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar ${JAR_FILE} --server.port=$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() {
  if [ -z $2 ]; then
    echo $"Usage: $0 $1 [port]"
    exit 2
  fi
  # -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] [port]"
  exit 2
elif [ -z $2 ]; then
  echo $"Usage: $0 {start|stop|restart|deploy} [some.jar] [port]"
  exit 2
fi
 
JAR_FILE=${RUN_BASE}/$2
# -e Check for file existence regardless of type (node, directory, socket)
[ -e ${JAR_FILE} ] || exit 1
 
case "$1" in
    start)
        if [ -z $3 ]; then
          echo $"Usage: $0 $1 [some.jar] [port]"
          exit 2
        fi
        $1 $2 $3
        ;;
    stop)
        $1 $2
        ;;
    restart)
        $1 $2 $3
        ;;
    deploy)
        if [ -z $3 ]; then
          echo $"Usage: $0 $1 [some.jar] [port]"
          exit 2
        fi
        $1 $2 $3
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|deploy} $2 [port]"
        exit 2
esac

posted on 2019-03-13 18:32  Milton  阅读(345)  评论(0编辑  收藏  举报

导航