為了做集群測試,在每台機器上裝了3個tomcat,每次發佈項目的時候都要反覆敲一些命令,重啟tomcat之前先檢查tomcat進程有沒有停掉,沒有還要手動kill該進程。 發佈次數多了,操作就比較繁瑣了,索性寫個腳本一鍵發佈,省時省力^_^。 把deploy.sh和restart.sh分別拷貝到3個 ...
為了做集群測試,在每台機器上裝了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
如需重啟tomcat則使用命令:./restart.sh 或 ./restart.sh -v (參數-v表示啟動時列印tomcat啟動日誌)
說明:restart.sh是用來重啟tomcat的,如果tomcat沒有啟動則直接啟動,如果已經啟動就先shutdown再啟動,如果shutdown之後3s沒有停掉tomcat進程,則kill掉原來的進程再啟動。
代碼如下:
#!/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