#!/bin/bash
# func:自动部署
# author:wuxinnian
# version 1.0
#tomcat实际安装路径
tomcatPath=/home/bmp/apache-tomcat-9.0.36
#项目名称
projectName=bmp-server
#获取tomcat的进程ID
tomcatID=$(ps -ef | grep /home/bmp/apache-tomcat-9.0.36|grep -v ‘grep‘|awk ‘{print $2}‘)
#启动tomcat
startTomcat=${tomcatPath}/bin/startup.sh
#webapps路径
webappsPath=${tomcatPath}/webapps
#获取当前的工作目录
workSpace=$(pwd)
#war文件
warfile="$projectName.war"
#主函数
function monitor()
{
echo "[info]开始监控tomcat...[$(date +‘%F %H:%M:%S‘)]"
if [ $tomcatID ];then
echo "[info]当前tomcat的进程ID为:$tomcatID"
kill -9 $tomcatID
fi
backup
removeProject
upload
startTomcat
}
#备份原来的项目
function backup()
{
echo "开始备份..."
fileDate=$(date "+%Y%m%d%H%M%S")
fileName=${projectName}${fileDate}
if [ -d "$webappsPath/$projectName" ];
then
cd $webappsPath
tar -zcvf ${fileName}.tar.gz ./${projectName}
cd $workSpace
echo "备份完毕"
else
echo "不存在$webappsPath/$projectName"
fi
}
#清除tomcat项目文件
function removeProject()
{
echo "开始清理tomcat项目文件..."
if [ -d "$webappsPath/$projectName" ];
then
rm -rf "$webappsPath/$projectName"
else
echo "不存在$webappsPath/$projectName"
fi
if [ -f "$webappsPath/$warfile" ];
then
rm -f "$webappsPath/$warfile"
else
echo "不存在$webappsPath/$warfile"
fi
echo "清理完毕"
}
#上传文件
function upload()
{
cd $webappsPath
if [ -f "$webappsPath/$warfile" ] ;
then
echo "已存在$warfile"
else
echo "上传文件..."
rz
fi
cd $workSpace
}
#启动tomcat
function startTomcat()
{
if [ -f "$webappsPath/$warfile" ];
then
cd ${tomcatPath}/bin/
echo "启动tomcat"
$startTomcat
tail -f ${tomcatPath}/logs/catalina.out
else
echo "未找到$warfile"
fi
}
monitor