jenkins tomcat 发布更新
2017-03-13 13:04
106 查看
之前设置了jenkins构建后deploy到tomcat的发布,但是,经常出现 OOM 错误,经常还要再ssh上去重启,很是不爽。断续研究执行远程ssh后,决定用shell来shutdown 和重启。实现需求如下:
1.如果应用已启动,则优先shutdown, shutdown无法关闭时,直接kill.
2.更新前,将原应用war按时间备份到备份目录.
3.清理webapps目录,更新war
4.启动,并输出日志,以便检查启动状态
5.启动后检查进程是否已开启,同时检查http请求系统是否已能正常访问。
脚本如下:
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 .."
相关文章推荐
- eclipse下tomcat发布更新文件(Could not delete xxx.data May be locked by another process)
- 图文讲解jenkins的安装与配置---远程发布、自动监测代码更新
- jenkins发布普通javaweb项目到tomcat
- Jenkins 下载SVN上的Maven项目发布到Tomcat下
- Jenkins 配置用户权限 + Tomcat 发布
- Tomcat重新发布项目后配置文件不更新的问题
- jenkins 自动从svn获取源码通过maven打包后发布到远端tomcat
- (踩坑记1)jenkins 编译完成后发布到异地tomcat8.5失败
- Jenkins+docker实现应用发布和更新
- Jenkins系列之——第四章 Jenkins编译一个Web项目并远程发布到Tomcat
- 基于Jenkins + Tomcat 的安卓客户端可持续化构建及发布下载(loltube.cn)
- maven+tomcat+jenkins+shell 实现从版本库源码(svn/git)一键编译打包及上线发布
- jenkins 远程执行shell 一键远程发布 tomcat
- jenkins自动发布后启动tomcat失败
- jenkins远程发布tomcat项目
- ANT发布SVN WEB项目到TOMCAT以及利用post-commit自动提交编译更新
- Linux配置Jenkins建立Maven Job持续发布项目到Tomcat
- jenkins发布到tomcat
- Jenkins将项目发布到Tomcat
- jdk+maven+svn+Tomcat+Jenkins项目部署发布