Tomcat 关闭报错:Tomcat did not stop in time. PID file w

1,报错信息如下:
[root@ptbin]# ./shutdown.sh
Using CATALINA_BASE:  /usr/local/app/apache-tomcat-6.0.37_9300
Using CATALINA_HOME:  /usr/local/app/apache-tomcat-6.0.37_9300
Using CATALINA_TMPDIR: /usr/local/app/apache-tomcat-6.0.37_9300/temp
Using JRE_HOME:        /usr/lib/jvm/Java
Using CLASSPATH:      /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar
Using CATALINA_PID:    /var/tomcat/9300.pid
Jun 12, 2015 4:55:30 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:422)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:338)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:416)

Tomcat did not stop in time. PID file was not removed.
[root@ptbin]#

2,去看下pid是否存在:
[root@pt~]# ll /var/tomcat/
total 12
-rw-r–r–. 1 root root 5 Jun 12 15:41 9300.pid
[root@pt~]#

tomcat的pid确实存在,与这个pid无关

3,解决办法:
解决办法有多种

3.1 解决办法1:修改java项目的实现方式
可能项目中用到了ScheduledThreadPoolExecutor(可能是未接受stop消息导致的),导致了shutdown命令未能成功。将ScheduledThreadPoolExecutor改成spring的quartz调度就ok了。

3.2 解决办法2,加上-force强行stop掉
在catalina.sh里面
exec “PRGDIR"/"EXECUTABLE” stop -force “$@”
大家查看shutdown.sh的代码:

[root@ptbin]# vim shutdown.sh

PRG="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh

# Check that target executable exists
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
  echo "Cannot find $PRGDIR/$EXECUTABLE"
  echo "The file is absent or does not have execute permission"
  echo "This file is needed to run this program"
  exit 1
fi

exec "$PRGDIR"/"$EXECUTABLE" stop "$@"
发现并没有echo “Tomcat did not stop in time. PID file was not removed.”的提示信息,那么报错信息肯定不是在shutdown.sh脚本里面,大家看到shutdown.sh最后有引用其它脚本exec "$PRGDIR"/"$EXECUTABLE" stop "$@"来关闭tomcat,再看到”$EXECUTABLE变量所指向的是catalina.sh,所以问题核心是在catalina.sh里面。

打开catalina.sh发现只有第453行里面有这样提示信息,如下:

433  if [ ! -z "$CATALINA_PID" ]; then
434    if [ -f "$CATALINA_PID" ]; then
435      while [ $SLEEP -ge 0 ]; do
436        kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
437        if [ $? -gt 0 ]; then
438          rm -f "$CATALINA_PID" >/dev/null 2>&1
439          if [ $? != 0 ]; then
440            if [ -w "$CATALINA_PID" ]; then
441              cat /dev/null > "$CATALINA_PID"
442            else
443              echo "Tomcat stopped but the PID file could not be removed or cleared."
444            fi
445          fi
446          break
447        fi
448        if [ $SLEEP -gt 0 ]; then
449          sleep 1
450        fi
451        if [ $SLEEP -eq 0 ]; then
452          if [ $FORCE -eq 0 ]; then
453            echo "Tomcat did not stop in time. PID file was not removed."
454          fi
455        fi
456        SLEEP=`expr $SLEEP - 1 `
457      done
458    fi

看到关键代码判断 if [ $FORCE -eq 0 ]; 那么这里就是表明前面执行异常,但是不是force强行停止掉,所以要抛一个exception出来,告诉你没有stop成功。

那么,我们可以在这里设置一个force命令来让tomcat强行stop掉。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4f0bdba19ba516edd3c44a3991017896.html