Fork me on GitHub
Suzf  Blog

Tomcat 自动更新脚本

之前总是要更新基于 Tomcat 的web项目,而且更新频繁。 手工更新太烦人了,遂写了一个半自动的脚本。已经一年多没接触 Tomcat ,脚本仅供参考 。

#!/bin/bash
# Author: Jeffery.Su
# Mail: [email protected]
# Site: http://suzf.net
# Date: Jun 1,2014
# Version: 1.0.2

# Definite variable
PRO_NAME=javacenter
WORK_PATH=/usr/local/tomcat/webapps
BACK_PATH=/opt/backup/${PRO_NAME}
UPLOAD_PATH=/root/$PRONAME
DATE=$(date '+%Y%m%d')
BACK_NAME="$PRO_NAME-$DATE.tar.gz"
#eval `date "+day=%d; month=%m; year=%Y"`
#BACK_NAME="$PRO_NAME-$year-$month-$day.tar.gz"

# Backup and Gzip the whole folder
[ -d $BACK_PATH ] || mkdir -p $BACK_PATH
cd $WORK_PATH
tar Ppzcf $BACK_PATH/$BACK_NAME $PRO_NAME
echo -e " $(date) Loading backup programmer, please waiting a moment....\n"
if [ $? -eq 0 ];then
echo -e " $(date) Backup the file succeed !\n"
else
echo -e " $(date) Backup the file failed ! please check !!!\n"
return 1;
fi

# Move the current webapps
mv $PRONAME/ $BACK_PATH/$BACK_NAME
rm -rvf $PRONAME.war

# Upload the new webapps
# Check the file complete upload
[ -d $UPLOAD_PATH ] || mkdir $UPLOAD_PATH
mv $UPLOAD_PATH/$PRO_NAME/* .
if [ "$?" == "0" ];then
echo "Please upload the webapps to $UPLOAD_PATH"
sleep 360;
fi

# Restart tomcat
echo -e " $(date) Stop tomcat\n"
../bin/shutdown.sh

i=0
while (($i <5)) do 
  PID=`ps aux|grep tomcat |grep -v "grep\|sh"|awk '{print $2}'` 
  if [[ "$PID" == "" ]];then
    echo "$PID not exits , tomcat hava stop success"
  else 
   echo "$PID is exits , tomcat is running" 
   let "i++" 
   sleep 5; 
   echo "$(date) Forced to stop tomcat" 
   # kill -9 $PID 
  fi 
done 
echo -e "$(date) start tomcat\n" 
../bin/startup.sh 
# Remove the old backups 
find $BACK_PATH -mtime +15 -exec rm -rf {}\; > /dev/null 2>&1

# Watch the log of catalina.out
#tailf ../logs/catalina.out