自动部署shell(结合Jenkins)

时间:2022-08-02 21:48:30


#!/bin/bash
NAME=(`ps -ef|grep tomcat|egrep -v '(grep|/bin/sh|\<tail)'|sed -r 's/^.*-Dcatalina.base=(.*)$/\1/'|awk '{print $1}'`)
NUM=`echo ${#NAME[*]}`
BACKUPPATH="/www/backup"
DIR="/www/target"
TIME=$(date +%Y%m%d%H%M)
#如果匹配多个tomcat会走到这个循环,如果是单个不会走到这里来
#######################################################
if [ $NUM -ne 1 ];then
for i in ${NAME[*]};do
if grep $1 $i/conf/server.xml &> /dev/null;then
NAME=$i
fi
done
fi
#######################################################
STARTPATH="$NAME/bin"
echo $STARTPATH
PID=`ps -ef|grep tomcat |grep $NAME/conf| grep -v "grep" | awk '{print $2}'`

#判断tomcat的pid是否为空,不为空就强制杀掉pid
echo "关闭tomcat服务"
if [ "$PID" == "" ]
then
echo "服务未启动!!"
else
echo "tomcat ID is :$PID"
kill -9 $PID
echo "tomcat服务已停止!!!"
fi

#备份
cd $DIR
echo "备份工程"
tar -zcf $BACKUPPATH/$1$TIME.tar.gz $1

#删除原来的工程文件
rm -rf $1
#将Jenkins的传过来的包放到tomcat容器下(Jenkins传来的包设置放到/root下面的)
mv /root/$1 $DIR

echo "准备启动tomcat服务!!"
cd $STARTPATH
./startup.sh
NEW_PID=`ps -ef|grep tomcat |grep $NAME/conf | grep -v "grep" | awk '{print $2}'`
if [ "$NEW_PID" == "" ]
then
echo "启动失败!!"
else
echo "启动成功,tomcat new id is:$NEW_PID"
fi