青岛免费建站,温州品牌设计公司,兖州网站建设,为什么网站建设需要每年续费最近我们在使用Jenkins自动化部署项目时#xff0c;在生产liunx环境下#xff0c;使用脚本shutdown.sh停止tomcat服务#xff0c;然后再start之后发现应用无法访问了#xff0c;后台查看tomcat进程是发现有个2个tomcat进程#xff0c;说明之前的shutdown并没有完全停掉tom…最近我们在使用Jenkins自动化部署项目时在生产liunx环境下使用脚本shutdown.sh停止tomcat服务然后再start之后发现应用无法访问了后台查看tomcat进程是发现有个2个tomcat进程说明之前的shutdown并没有完全停掉tomcat进程。那怎么样tomcat使用shutdown之后立马关掉其进程呢 经查资料发现在shutdown.sh脚本之后有条命令是这样的exec $PRGDIR/$EXECUTABLE stop $这个就是停止tomcat 服务的命令我们只需要加一个 -force 就可以在shutdown时强制关闭tomcat进程exec $PRGDIR/$EXECUTABLE stop -force $但光这样还是不行再shutdown时报错Kill failed: $CATALINA_PID not set查找失败原因 FORCE1 [ ! -z ] /usr/java/jdk1.6.0_38/bin/java -server -Xms2048m -Xmx2048m -Xmn768m -XX:PermSize128m -XX:MaxPermSize256m -XX:UseParallelOldGC -XX:PrintGCDateStamps -XX:PrintGCDetails -XX:HeapDumpOnOutOfMemoryError -XX:HeapDumpPath/home/xrltest1/tomcat/dumpfile/heap.bin -Xloggc:/home/xrltest1/tomcat/logs/gc.log -Djava.util.logging.managerorg.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs/home/xrltest1/tomcat/endorsed -classpath /home/xrltest1/tomcat/bin/bootstrap.jar -Dcatalina.base/home/xrltest1/tomcat -Dcatalina.home/home/xrltest1/tomcat -Djava.io.tmpdir/home/xrltest1/tomcat/temp org.apache.catalina.startup.Bootstrap stop2015-3-21 11:59:53 org.apache.catalina.startup.Catalina stopServer严重: Catalina.stop:java.net.ConnectException: Connection refusedat java.net.PlainSocketImpl.socketConnect(Native Method)at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)at java.net.Socket.connect(Socket.java:529)at java.net.Socket.connect(Socket.java:478)at java.net.Socket.(Socket.java:375)at java.net.Socket.(Socket.java:189)at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:422)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:338)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:416) [ ! -z ] [ 1 -eq 1 ] [ -z ] echo Kill failed: $CATALINA_PID not setKill failed: $CATALINA_PID not set从中可以看到首先是执行java命令失败没有停止tomcat然后执行-force模块的命令但是却没有找到$CATALINA_PID设定的进程号我们先不去关注java执行stop命令为什么报错而是看看为什么加了-force参数也不起作用了为什么没有$CATALINA_PID接下来就带大家一探究竟首先我们将涉及到$CATALINA_PID变量的代码全部提取出来第一处#以下这句判断设置的$CATALINA_PID变量如果不存在则显示Using CATALINA_PID:$CATALINA_PID如果存在则不显示if [ ! -z $CATALINA_PID ]; thenecho Using CATALINA_PID: $CATALINA_PIDfifi#貌似只是判断$CATALINA_PID是否是空字符其他什么都没有做第二处(涉及到start参数的模块内的$CATALINA_PID变量只是提出来分析其实不对stop模块有影响)#这个是start模块内的代码$_RUNJAVA $LOGGING_CONFIG $JAVA_OPTS $CATALINA_OPTS \-Djava.endorsed.dirs$JAVA_ENDORSED_DIRS -classpath $CLASSPATH \-Dcatalina.base$CATALINA_BASE \-Dcatalina.home$CATALINA_HOME \-Djava.io.tmpdir$CATALINA_TMPDIR \org.apache.catalina.startup.Bootstrap $ start \ $CATALINA_OUT 21 #从可以看出启动的命令在后台启动fiif [ ! -z $CATALINA_PID ]; then#判断CATALINA_PID如果不是空字符则将Shell最后运行的后台Process的PID 传给$CATALINA_PIDecho $! $CATALINA_PID#在使用命令运行进程至后台时可以使用$!抓取前面启动运行在后台进程的进程号fifi#上面语句是tomcat在启动时会将$CATALINA_PID写入PID进程号第三处#一下语句都出现在stop模块内if [ ! -z $CATALINA_PID ]; then#$CATALINA_PID文件不是非空if [ -f $CATALINA_PID ]; thenif [ -s $CATALINA_PID ]; thenkill -0 cat $CATALINA_PID /dev/null 21#kill -0 pid 不发送任何信号但是系统会进行错误检查。if [ $? -gt 0 ]; thenecho PID file found but no matching process was found. Stop aborted.exit 1fielseecho PID file is empty and has been ignored.fielseecho \$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.exit 1fifi#如果发现$CATALINA_PID则发送一个模拟结束进程的型号如果返回值为0则正常其他则异常做退出操作#从上面的sh -x输出的信息对应的是[ ! -z ] 可以看出$CATALINA_PID变量没有设定所以这个判断直接就结束了什么都没做第四处#下面的代码紧接着stop的正常停止代码下if [ ! -z $CATALINA_PID ]; then#如果$CATALINA_PID不为空if [ -f $CATALINA_PID ]; then#而且还是普通文件while [ $SLEEP -ge 0 ]; do#而且$SLEEP还大于0kill -0 cat $CATALINA_PID /dev/null 21#则测试下tomcat能不能被关闭if [ $? -gt 0 ]; then#剩下的参数还有则清空$CATALINA_PIDrm -f $CATALINA_PID /dev/null 21if [ $? ! 0 ]; thenif [ -w $CATALINA_PID ]; thencat /dev/null $CATALINA_PIDelseecho Tomcat stopped but the PID file could not be removed or cleared.fifibreakfiif [ $SLEEP -gt 0 ]; thensleep 1fiif [ $SLEEP -eq 0 ]; thenif [ $FORCE -eq 0 ]; thenecho Tomcat did not stop in time. PID file was not removed.fifiSLEEPexpr $SLEEP - 1 donefifi#上段语句主要是判断tomcat是否被关闭#核心语句还是kill -0 cat $CATALINA_PID /dev/null 21#while语句做sleep使用用于清空$CATALINA_PID#但是[ ! -z $CATALINA_PID ]导致这段语句什么都没做啊第五处#这段代码就是涉及-force的核心代码了if [ $FORCE -eq 1 ]; thenif [ -z $CATALINA_PID ]; thenecho Kill failed: \$CATALINA_PID not setelseif [ -f $CATALINA_PID ]; thenPIDcat $CATALINA_PIDecho Killing Tomcat with the PID: $PIDkill -9 $PID#强制执行的核心命令rm -f $CATALINA_PID /dev/null 21if [ $? ! 0 ]; thenecho Tomcat was killed but the PID file could not be removed.fifififi#从中就可以看出sh -x输出的Kill failed: $CATALINA_PID not set是怎么来的总结下$CATALINA_PID在整个代码中的作用1.在tomcat启动时会写入$CATALINA_PID但是假设我们的环境是多tomcat项目或$CATALINA_PID为空2.stop代码中检查$CATALINA_PID是否为空字符是的话什么都不做3.sstop代码中检查$CATALINA_PID是否为空字符是的话什么都不做4.force代码中检查$CATALINA_PID是否为空字符是的话就报错也就是说只要没有保存中这个项目的PID那么正常stop停止不了-force也是没有用的。那么该如何解决呢直接给$CATALINA_PID付PID的值那么看结果 FORCE1 [ ! -z 12031 ] [ -f 12031 ] echo $CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.涉及到的代码if [ -f $CATALINA_PID ]; thenif [ -s $CATALINA_PID ]; thenkill -0 cat $CATALINA_PID /dev/null 21#kill -0 pid 不发送任何信号但是系统会进行错误检查。if [ $? -gt 0 ]; thenecho PID file found but no matching process was found. Stop aborted.exit 1fielseecho PID file is empty and has been ignored.fielseecho \$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.exit 1#$CATALINA_PID不是个文件所以不好意思我退出了....原来$CATALINA_PID还必须是个文件那么我们可不可以建这么一个文件呢在catalina.sh脚本的代码前加入以下语句#########################################################################################if [ -z $CATALINA_PID ]; thenCATALINA_PID$PRGDIR/CATALINA_PIDcat $CATALINA_PIDfi######################################################################################结果tomcat停止执行成功完成结束进程任务总结在我看到的很到部署tomcat的文章中还没发现有关于设置$CATALINA_PID件路径的提示(也许是我看到的少)但是此处我要建议小伙伴们在写tomcat启动、停止、重启的脚本的时候一定要注意这个变量。更多Tomcat相关教程见以下内容Tomcat 的详细介绍请点这里Tomcat 的下载地址请点这里