用于在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
原文地址:https://www.cnblogs.com/milton/p/10525051.html
时间: 2024-10-10 02:31:13