之前设置了jenkins构建后deploy到tomcat的发布,但是,经常出现 OOM 错误,经常还要再ssh上去重启,很是不爽。断续研究执行远程ssh后,决定用shell来shutdown 和重启。实现需求如下:
1.如果应用已启动,则优先shutdown, shutdown无法关闭时,直接kill.
2.更新前,将原应用war按时间备份到备份目录.
3.清理webapps目录,更新war
4.启动,并输出日志,以便检查启动状态
5.启动后检查进程是否已开启,同时检查http请求系统是否已能正常访问。
脚本如下:
#!/bin/bash
# 发布时重启启动tomcat脚本
export JAVA_HOME=/opt/java/jdk1.7
serverHome="/opt/ci/tttt/"
startup="bin/startup.sh"
shutdown="bin/shutdown.sh"
dev="/dev/null"
# 定义要监控的页面地址
serverUrl=http://www.baidu.com/
echo "tomcat的sh为$serverSh"
echo "tomcat的url为$serverUrl"
# 获取tomcat进程ID
serverId=$(ps -ef |grep tomcat |grep -w 'tttt'|grep -v 'grep'|awk '{print $2}')
echo "tomcat的pid为$serverId"
#check the thread is exist ,if exist ,return 0 ,else return 1
function checkThread(){
echo "..[checkThread]check Java Tomcat thread is exist !"
tmpServerId=$(ps -ef |grep tomcat |grep -w 'tttt'|grep -v 'grep'|awk '{print $2}')
echo "..[checkThread]check Java Tomcat thread :${tmpServerId}"
if [ $tmpServerId ]
then
echo "..[checkThread] has this Java tomcat thread"
return 0
fi
echo "..[checkThread] has no Java tomcat thread"
return 1
}
#shutdown the thread
function shutdown(){
tmpServerId=$(ps -ef |grep tomcat |grep -w 'tttt'|grep -v 'grep'|awk '{print $2}')
echo "..[checkThread]check Java Tomcat thread :${tmpServerId}"
if [ $tmpServerId ]
then
${serverHome}/${shutdown} &>$dev
echo "tomcat $tmpServerId stopping,Please wait 5s..."
sleep 5
fi
echo "..tomcat $tmpServerId shutdown .."
}
#killThreadWhatever
function killThreadWhatever(){
echo ".. [killThreadWhatever]now kill the thread .."
tmpServerId=$(ps -ef |grep tomcat |grep -w 'tttt'|grep -v 'grep'|awk '{print $2}')
echo ".. [killThreadWhatever]check Java Tomcat thread :${tmpServerId}"
if [ $tmpServerId ]
then
echo ".. [killThreadWhatever] executing kill "
kill -9 $tmpServerId
tmpServerId=$(ps -ef |grep tomcat |grep -w 'tttt'|grep -v 'grep'|awk '{print $2}')
if [[ $tmpServerId ]]
then
echo ".. [killThreadWhatever] executed kill ,failed"
return 1
fi
echo ".. [killThreadWhatever] executed kill ,OK "
return 0
fi
echo ".. [killThreadWhatever] has no Java tomcat thread return 0"
return 0
}
#shutdown if nessensary
echo "..【step 1 shutdown/kill】 begin.."
shutdown
checkThread
checkThreadFlag=$?
if [ $checkThreadFlag -ne 0 ]
then
echo ".. shutdown effective .."
else
#killThread
killThreadWhatever
echo -e "@ shutdown not effect ,now killed , result is :$? \n"
fi
echo "..【step 1 shutdown/kill】 end.."
echo -e "\n\n..【step 2 update/start】 begin.."
#clean war
echo -e "==[clean move 1/3] next will update project \n .. will clean and mv excute !"
rm -rf ${serverHome}/webapps/*
echo "..[clean move 2/3] rm done ! flag : $?"
cp ${serverHome}/web.war ${serverHome}/webapps/
echo "..[clean move 3/3] move done ! flag : $?"
#sleep seconds
sleep 1s
#startup
echo -e "\n\n==[start 1/2] next will startup excute !"
nohup ${serverHome}/${startup} & >output 2&1
echo -e "\n\n==[start 2/2] startup excuted !"
echo -e "\n\n..【step 2 update/start】 end.."
sleep 20s
#sleep 20s
tail -n150 ${serverHome}/logs/catalina.out
echo -e "\n\n..【step 3 check】 begin.."
checkThread
recheckThread=$?
if [ $recheckThread -eq 0 ]
then
echo "..[restarted end] check startuped .."
else
echo "..[restarted end] check startuped error? .."
fi
# 检测是否启动成功(成功的话页面会返回状态"200")
httpCheckCode=$(curl -s -o $dev -m 10 --connect-timeout 10 $serverUrl -w %{http_code})
#httpCheckCode=$(curl -s -o /dev/null -m 10 --connect-timeout 10 http://www.baidu.com/ -w %{http_code})
#echo $httpCheckCode
if [ $httpCheckCode -eq 200 ];then
echo "..[restarted end] check page , 测试页面正常......"
else
echo "..[restarted end] check page , 测试页面异常,请检查系统是否正常启动......"
fi
echo -e "\n\n..【step 3 check】 end.."
echo "..[end] DONE .."