shell实现tomcat自动备份重启

来源:互联网 发布:什么软件交易外汇 编辑:程序博客网 时间:2024/05/01 07:28
运行环境:
    Tomcat:Apache Tomcat Version 7.0.25
    Server:CentOS release 6.2


作为一名web程序员或测试人员,需要经常发布程序到线上环境或测试环境中。
每次向线上或者测试环境发布新war包的时候,我一般都这么做
1、put xxx.war 到 tomcat_xxx(一台服务器上有多个tomcat,比如项目名称是xxx,tomcat一般命名为tomcat_xxx)
2、ps -ef | grep tomcat_xxx 找到对应的tomcat进程
3、kill tomcat进程
4、rm掉 webapps下的xxx目录(在tomcat启动时由xxx.war生成的)
5、mv xxx.war 到指定的备份目录中
7、mv 新上传的xxx.war 到 webapps中
8、执行startup.sh,启动tomcat
9、tail -f catalina.out 查看启动日志输出,确保启动成功。

现在有了auto_deploy.sh,将auto_deploy.sh放在tomcat的bin目录下,
只需要以下两步(以我的环境为例)
1、put xxx.war 到 /work/tomcat_xxx
2、执行auto_deploy.sh

auto_deploy.sh全部代码如下:(注意:首次运行需要指定PRO_NAME

#!/bin/sh#项目名称,一般是war包的名称,例如xxx.war,则PRO_NAME="xxx"PRO_NAME=""#shell 文件所在目录BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"#tomcat 根目录TOMCAT_HOME="$(dirname ${BIN_DIR})"#tomcat 进程名称,一般是ps -ef |grep xxx 中的xxxPRG="$(basename ${TOMCAT_HOME})"DATE=`date +%Y%m%d%H%M%S`if [ "$PRO_NAME" = "" ]; then    echo "ERROR:未配置项目名称,请配置 PRO_NAME 参数值!!!"    exit 0fi#kill tomcat 进程ps -ef |grep "$PRG" | grep "jdk" | awk '{print "kill -9 " $2}' | sh#生成一个备份目录if [ ! -d "$TOMCAT_HOME/bak" ]; then    mkdir $TOMCAT_HOME/bakfi#部署新war包,同时将旧war包备份并删除旧的程序文件if [ -f "$TOMCAT_HOME/$PRO_NAME.war" ]; then    if [ -f "$TOMCAT_HOME/webapps/$PRO_NAME.war" ]; then        mv $TOMCAT_HOME/webapps/$PRO_NAME.war $TOMCAT_HOME/bak/${PRO_NAME}_$DATE.war    fi    rm -rf $TOMCAT_HOME/webapps/$PRO_NAME    mv $TOMCAT_HOME/$PRO_NAME.war $TOMCAT_HOME/webapps/fi#启动tomcatnohup $TOMCAT_HOME/bin/startup.sh > $TOMCAT_HOME/logs/catalina.out &#打印启动日志tail -f $TOMCAT_HOME/logs/catalina.out

原创粉丝点击