简单的tomcat自动化部署脚本,实现功能:
- 检查tomcat进程是否存在,如果存在则kill掉
- 备份现有war包到tomcat/backup目录
- 复制当前目录新war包到tomcat/webapps目录
- 启动tomcat
#!/bin/bash
now=`date +%Y%m%d%H%M%S`
tomcatPath=/opt/tomcat
backupPath=/opt/tomcat/backup
war=$1
if [ -e "$war.war" ]; then
echo -e "\033[34m war archive: $war.war \033[0m"
else
echo -e "\033[31m war archive '$war.war' not exists \033[0m"
exit -1
fi
# change color
echo -e "\033[34m"
#create backup dir
if [ ! -d "$backupPath" ]; then
mkdir "$backupPath"
fi
echo "tomcat home: $tomcatPath"
echo "backup path: $backupPath"
echo 'try to stop tomcat...'
pid=`ps aux|grep "java"|grep "$tomcatPath"|awk '{printf $2}'`
if [ -n $pid ]; then
echo "tomcat pid: $pid";
kill -9 $pid;
fi
echo 'stop tomcat finished...'
echo 'backup old archive...'
if [ -f "$tomcatPath/webapps/$war.war" ]; then
mv -v "$tomcatPath/webapps/$war.war" "$backupPath/$1_$now.war";
fi
rm -rf $tomcatPath/webapps/$war*
echo "copy $war.war archive to webapps.."
cp -v "$war.war" "$tomcatPath/webapps/"
echo -e "\033[32m"
echo 'startup tomcat...'
sh $tomcatPath/bin/startup.sh
tail -10f $tomcatPath/logs/catalina.out
注意:先修改tomcatPath的值为实际tomcat路径
保存该文件到autodeploy.sh,执行命令
./autodeploy.sh abc
abc为待发货的war包名称,即当前目录存在两个文件:
autodeploy.sh abc.war
因为自己只是touch了一个autodeploy.sh的文件,不是可执行文件,所以要加权限
chmod +x autodeploy.sh
此时就可以执行
./autodeploy.sh pos-web
日志输出,哈哈哈.....