用于Spring Boot Jar部署的shell脚本

Posted Milton

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于Spring Boot Jar部署的shell脚本相关的知识,希望对你有一定的参考价值。

用于在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

  

 

以上是关于用于Spring Boot Jar部署的shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat使用Tomcat部署Spring Boot项目生成的jar包

通用Shell脚本执行Spring Boot项目Jar包

nginx 部署多个 spring-boot jar 方式项目

Spring Boot 部署浅析(jar or war)

Spring Boot打包部署修改jar文件名

Java之Spring Boot入门到精通,Spring Boot项目部署jar包方式,war包方式(一篇文章精通系列)