#!/bin/bashjava
#definedweb
now=`date +%Y%m%d%H%M%S`shell
TOMCAT_HOME="/usr/tomcat7/apache-tomcat-7.0.70/"apache
TOMCAT_PORT=8080tomcat
PROJECT="$1"bash
BACKUP_DIR="/tomcat_back/$PROJECT/$now"app
#param validate;webapp
if [ $# -lt 1 ]; thenide
echo "you must use like this : ./publish.sh <projectname> [tomcat port] [tomcat home dir]" 工具
exit
fi
if [ "$2" != "" ]; then
TOMCAT_PORT=$2
fi
if [ "$3" != "" ]; then
TOMCAT_HOME="$3"
fi
#shutdown tomcat
"$TOMCAT_HOME"/bin/shutdown.sh
echo "tomcat shutdown"
#check tomcat process
# tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`
tomcat_pid=`ps aux|grep "java"|grep "$TOMCAT_HOME"|awk '{printf $2}'`
while [ -n "$tomcat_pid" ]
do
echo "current :" $tomcat_pid
kill -9 $tomcat_pid
echo "scan tomcat pid :" $tomcat_pid
done
#publish project
echo "scan no tomcat pid,$PROJECT publishing"
rm -rf "$TOMCAT_HOME"/webapps/$PROJECT*
cp /save/$PROJECT*.war "$TOMCAT_HOME"/webapps/$PROJECT.war
#bak project and may be create backup dir
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
fi
cp "$TOMCAT_HOME"/webapps/$PROJECT.war "$BACKUP_DIR"/"$PROJECT"_`date +%H%M%S`.war
#remove tmp
rm -rf /save/$PROJECT*.war
#start tomcat
"$TOMCAT_HOME"/bin/startup.sh
tail -10f "$TOMCAT_HOME/logs/catalina.out"
echo "#############################################################"
echo "tomcat is starting,please try to access $PROJECT conslone url"
echo "#############################################################"
每一步的註釋還算清楚,由於公司項目自己沒有采用專業的源碼管理工具來管理項目,因此此腳本還要準備好相應的不一樣平臺的配置文件,須要更改默認的項目配置文件,若是有專業的源碼管理,那就不用這麼囉嗦了,直接使用該腳本就能夠部署tomcat的工程了
######################## 如下爲更新後的腳本 ##########################
#!/bin/bash
#defined
TOMCAT_HOME="/usr/tomcat7/apache-tomcat-7.0.70"
TOMCAT_PORT=8080
PROJECT="$1"
#param validate
if [ $# -lt 1 ]; then
echo "you must use like this : ./publish.sh <projectname> [tomcat port] [tomcat home dir]"
exit
fi
if [ "$2" != "" ]; then
TOMCAT_PORT=$2
fi
if [ "$3" != "" ]; then
TOMCAT_HOME="$3"
fi
#shutdown tomcat
"$TOMCAT_HOME"/bin/shutdown.sh
echo "tomcat shutdown"
#check tomcat process
tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`
echo "current :" $tomcat_pid
while [ -n "$tomcat_pid" ]
do
tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`
kill -9 $tomcat_pid
echo "scan tomcat pid :" $tomcat_pid
done
#publish project
echo "scan no tomcat pid,$PROJECT publishing"
rm -rf "$TOMCAT_HOME"/webapps/$PROJECT*
cp /save/$PROJECT*.war "$TOMCAT_HOME"/webapps/$PROJECT.war
#bak project
BAK_DIR=/save/bak/$PROJECT/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "$TOMCAT_HOME"/webapps/$PROJECT.war "$BAK_DIR"/"$PROJECT"_`date +%H%M%S`.war
#remove tmp
rm -rf /save/$PROJECT*.war
#start tomcat
"$TOMCAT_HOME"/bin/startup.sh
sleep 5
#echo "tomcat is starting,please try to access $PROJECT conslone url"
### change the configure file ###因爲在shell腳本中處於非交互模式,因此alias功能是未開啓的,而後強制複製項目的配置文件到tomcat應用裏面
shopt -s expand_aliases
unalias cp
cp -af /save/configfile/* /usr/tomcat7/apache-tomcat-7.0.70/webapps/manage/WEB-INF/classes/
### restart tomcat application
"$TOMCAT_HOME"/bin/shutdown.sh
### check tomcat process agion
tomcat_pid=`ps aux|grep "java"|grep "$TOMCAT_HOME"|awk '{print $2}'`
if [ -n "$tomcat_pid" ];then
echo "current :" $tomcat_pid
kill -9 $tomcat_pid
else
echo "no tomcat process"
fi
### start tomcat
"$TOMCAT_HOME"/bin/startup.sh
echo "###################################################"
echo "# tomcat is starting.pleage check the logs for it #"
echo "###################################################"
到此,此腳本告一段落,注意此腳本備份的是上傳的項目,而非上次的項目,若是本身有需求,要注意配置相關部分