准备:
- 提前修改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