準備:
- 提前修改war包里的相關配置,并上傳到服務器;
- 根據要自動升級的tomcat應用修改或添加腳本相關內容;
- tomcat啟動腳本如是自己寫的,要統一格式命名,如:xxx、xxxTomcat 等;
- 拿到生產使用前應在測試環境測試驗證。
腳本:
#!/bin/bash# Program:#This script is used for update tomcat.# History:# 2019/12/05YosonFirst releaseexport tomcat1="/opt/run/tomcat1"export tomcat2="/opt/run/tomcat2"txt="tomcat1|tomcat2 warFile"update() { echo "Update..." #file=${2##*/} #echo file=$file dir=${file%.*}#echo $dir tomcat=$(eval echo $$1)#echo tomcat=$tomcat sourceDir=$tomcat/webapps/$dir#echo sourceDir=$sourceDir destinationDir=`dirname $2`#echo destinationDir=$destinationDir sourceFile=${sourceDir}.war#echo sourceFile=$sourceFile /etc/init.d/${1}Tomcat stop 2>/dev/null [ -d $sourceDir ] && mv $sourceDir $destinationDir/${dir}.`date -I` [ -f $sourceFile ] && rm -f $sourceFile cp -f $2 $tomcat/webapps/ /etc/init.d/${1}Tomcat start}# 如果參數有兩個,則往下執行,否則提示使用說明if [ $# = '2' ];then export file=${2##*/} #echo file=$file #echo ${file#*.} # 如果參數2存在且為正常的war文件,則往下執行,否則提示文件不存在或不合法 if [ -f $2 -a "${file#*.}" = "war" ];thencase "$1" in tomcat1|tomcat2)update $1 $2;; *)echo "Usage: $0 $txt"exit 1esac else echo "$0: cannot stat ‘$2’: No such file or the file is not a war file" fielse echo "Usage: $0 $txt"fi
