為了做叢集測試,在每台機器上裝了3個tomcat,每次釋出項目的時候都要反複敲一些指令,重新開機tomcat之前先檢查tomcat程序有沒有停掉,沒有還要手動kill該程序。
釋出次數多了,操作就比較繁瑣了,索性寫個腳本一鍵釋出,省時省力^_^。
把deploy.sh和restart.sh分别拷貝到3個tomcat的bin目錄下,再用chmod +x給這兩個腳本賦上可執行權限。
一鍵釋出指令:./deploy.sh 項目war包 例如:./deploy.sh /home/test.war
說明:deploy.sh會先清空tomcat下的ROOT目錄,再将指定的war包加壓至ROOT目錄,最後執行restart.sh重新開機tomcat。
代碼如下:
#!/bin/sh
war=$1
bin=$(cd `dirname $0`; pwd)
if [ ! -n "${war}" ]; then
echo "***Usage: $0 [project.war]"
exit 0
fi
if [ ! -f "${war}" ]; then
echo "***Error: ${war} does not exist."
exit 0
fi
if [ ! "${war##*.}" = "war" ]; then
echo "***Error: ${war} is not a war file."
exit 0
fi
echo "Deploy ${war##*/}..."
rm -rf ${bin}/../webapps/ROOT/ && unzip -qo ${war} -d ${bin}/../webapps/ROOT/
rm -rf ${bin}/../work/Catalina/localhost/
echo "Restart tomcat..."
exec ${bin}/restart.sh
說明:restart.sh是用來重新開機tomcat的,如果tomcat沒有啟動則直接啟動,如果已經啟動就先shutdown再啟動,如果shutdown之後3s沒有停掉tomcat程序,則kill掉原來的程序再啟動。
如需重新開機tomcat則使用指令:./restart.sh 或 ./restart.sh -v (參數-v表示啟動時列印tomcat啟動日志)
#!/bin/sh
bin=$(cd `dirname $0`; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
echo "Shutdown..."
sh ${bin}/shutdown.sh
sleep 3
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
kill -9 ${pid}
sleep 1
fi
fi
echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
tail -f -n 600 ${bin}/../logs/catalina.out
fi
如果Tomcat 配置了日志切割的話:
#!/bin/sh
bin=$(cd `dirname $0`; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
echo "Shutdown..."
sh ${bin}/shutdown.sh
sleep 3
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
kill -9 ${pid}
sleep 1
fi
fi
echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
tail -f -n 600 ${bin}/../logs/$(date +%Y-%m-%d).catalina.out
fi
原文連結:
https://www.linuxidc.com/Linux/2016-01/127258.htm