码迷,mamicode.com
首页 > 编程语言 > 详细

用于Spring Boot Jar部署的shell脚本

时间:2019-03-13 19:37:26      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:java_home   str   ash   port   use   pre   check   cas   max   

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

标签:java_home   str   ash   port   use   pre   check   cas   max   

原文地址:https://www.cnblogs.com/milton/p/10525051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!