共计 17581 个字符,预计需要花费 44 分钟才能阅读完成。
背景:执行 /shutdown.sh 偶尔会报错如下,但是大部分时间还是比较正常的,希望能解决掉它
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 掉。
[root@ptbin]# vim shutdown.sh
exec “$PRGDIR”/”$EXECUTABLE” stop “$@”
# 改成如下
exec “$PRGDIR”/”$EXECUTABLE” stop -force “$@”
# 然后再执行 shutdown.sh 操作,ok,就不会报错,正常执行完毕了。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
[root@ptbin]#
3.3 通过 bash - x 分析真正的原因
3.3.1 先查看执行报错的时候的 bash 过程
[root@ptbin]# bash -x catalina.sh stop
+ JAVA_OPTS=’-Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+PrintGCDetails -Xloggc:/logs/gc/9300.gc’
+ CATALINA_PID=/var/tomcat/9300.pid
+ cygwin=false
+ os400=false
+ darwin=false
+ case “`uname`” in
++ uname
+ PRG=catalina.sh
+ ‘[‘ -h catalina.sh ‘]’
++ dirname catalina.sh
+ PRGDIR=.
+ ‘[‘ -z ” ‘]’
++ cd ./..
++ pwd
+ CATALINA_HOME=/usr/local/app/apache-tomcat-6.0.37_9300
+ ‘[‘ -z ” ‘]’
+ CATALINA_BASE=/usr/local/app/apache-tomcat-6.0.37_9300
+ CLASSPATH=
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setenv.sh ‘]’
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setenv.sh ‘]’
+ false
+ false
+ false
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh ‘]’
+ BASEDIR=/usr/local/app/apache-tomcat-6.0.37_9300
+ . /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh
++ ‘[‘ -z /usr/lib/jvm/java -a -z ” ‘]’
++ ‘[‘ -z /usr/lib/jvm/java -a stop = debug ‘]’
++ ‘[‘ -z ” ‘]’
++ JRE_HOME=/usr/lib/jvm/java
++ ‘[‘ stop = debug ‘]’
++ ‘[‘ -z /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
++ ‘[‘ ‘!’ -x /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh ‘]’
++ ‘[‘ -z ” ‘]’
++ JAVA_ENDORSED_DIRS=/usr/local/app/apache-tomcat-6.0.37_9300/endorsed
++ JIKESPATH=
+++ uname -s
++ ‘[‘ Linux = Darwin ‘]’
++ _RUNJAVA=/usr/lib/jvm/java/bin/java
++ ‘[‘ false ‘!=’ true ‘]’
++ _RUNJDB=/usr/lib/jvm/java/bin/jdb
+ ‘[‘ -z /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
+ ‘[‘ ‘!’ -z ” ‘]’
+ ‘[‘ /usr/local/app/apache-tomcat-6.0.37_9300 ‘!=’ /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
+ CLASSPATH=/usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar
+ ‘[‘ -z ” ‘]’
+ CATALINA_OUT=/usr/local/app/apache-tomcat-6.0.37_9300/logs/catalina.out
+ ‘[‘ -z ” ‘]’
+ CATALINA_TMPDIR=/usr/local/app/apache-tomcat-6.0.37_9300/temp
+ have_tty=0
++ tty
+ ‘[‘ /dev/pts/1 ‘!=’ ‘not a tty’ ‘]’
+ have_tty=1
+ false
+ ‘[‘ -z ” ‘]’
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/conf/logging.properties ‘]’
+ LOGGING_CONFIG=-Djava.util.logging.config.file=/usr/local/app/apache-tomcat-6.0.37_9300/conf/logging.properties
+ ‘[‘ -z ” ‘]’
+ LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
+ ‘[‘ 1 -eq 1 ‘]’
+ echo ‘Using CATALINA_BASE: /usr/local/app/apache-tomcat-6.0.37_9300’
Using CATALINA_BASE: /usr/local/app/apache-tomcat-6.0.37_9300
+ echo ‘Using CATALINA_HOME: /usr/local/app/apache-tomcat-6.0.37_9300’
Using CATALINA_HOME: /usr/local/app/apache-tomcat-6.0.37_9300
+ echo ‘Using CATALINA_TMPDIR: /usr/local/app/apache-tomcat-6.0.37_9300/temp’
Using CATALINA_TMPDIR: /usr/local/app/apache-tomcat-6.0.37_9300/temp
+ ‘[‘ stop = debug ‘]’
+ echo ‘Using JRE_HOME: /usr/lib/jvm/java’
Using JRE_HOME: /usr/lib/jvm/java
+ echo ‘Using CLASSPATH: /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar’
Using CLASSPATH: /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ echo ‘Using CATALINA_PID: /var/tomcat/9300.pid’
Using CATALINA_PID: /var/tomcat/9300.pid
+ ‘[‘ stop = jpda ‘]’
+ ‘[‘ stop = debug ‘]’
+ ‘[‘ stop = run ‘]’
+ ‘[‘ stop = start ‘]’
+ ‘[‘ stop = stop ‘]’
+ shift
+ SLEEP=5
+ ‘[‘ ‘!’ -z ” ‘]’
+ FORCE=0
+ ‘[‘ ” = -force ‘]’
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ ‘[‘ -f /var/tomcat/9300.pid ‘]’
+ ‘[‘ -s /var/tomcat/9300.pid ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ echo 0000002
0000002
+ /usr/lib/jvm/java/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+PrintGCDetails -Xloggc:/logs/gc/9300.gc -Djava.endorsed.dirs=/usr/local/app/apache-tomcat-6.0.37_9300/endorsed -classpath /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar -Dcatalina.base=/usr/local/app/apache-tomcat-6.0.37_9300 -Dcatalina.home=/usr/local/app/apache-tomcat-6.0.37_9300 -Djava.io.tmpdir=/usr/local/app/apache-tomcat-6.0.37_9300/temp org.apache.catalina.startup.Bootstrap stop
Jun 13, 2015 9:27:51 AM 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)
+ echo 0000003
0000003
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ echo CATALINA_PID:/var/tomcat/9300.pid
CATALINA_PID:/var/tomcat/9300.pid
+ ‘[‘ -f /var/tomcat/9300.pid ‘]’
+ ‘[‘ 5 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 5 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 5 -eq 0 ‘]’
++ expr 5 – 1
+ SLEEP=4
+ ‘[‘ 4 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 4 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 4 -eq 0 ‘]’
++ expr 4 – 1
+ SLEEP=3
+ ‘[‘ 3 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 3 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 3 -eq 0 ‘]’
++ expr 3 – 1
+ SLEEP=2
+ ‘[‘ 2 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 2 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 2 -eq 0 ‘]’
++ expr 2 – 1
+ SLEEP=1
+ ‘[‘ 1 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 1 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 1 -eq 0 ‘]’
++ expr 1 – 1
+ SLEEP=0
+ ‘[‘ 0 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23430
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 0 -eq 0 ‘]’
+ ‘[‘ 0 -eq 0 ‘]’
+ echo ‘Tomcat did not stop in time. PID file was not removed.’
Tomcat did not stop in time. PID file was not removed.
++ expr 0 – 1
+ SLEEP=-1
+ ‘[‘ -1 -ge 0 ‘]’
+ ‘[‘ 0 -eq 1 ‘]’
[root@ptbin]#
通过设置 echo 提示信息,找到报错执行语句,大家看到报错的真正原因是在于执行这一句:
/usr/lib/jvm/java/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+PrintGCDetails -Xloggc:/logs/gc/9300.gc -Djava.endorsed.dirs=/usr/local/app/apache-tomcat-6.0.37_9300/endorsed -classpath /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar -Dcatalina.base=/usr/local/app/apache-tomcat-6.0.37_9300 -Dcatalina.home=/usr/local/app/apache-tomcat-6.0.37_9300 -Djava.io.tmpdir=/usr/local/app/apache-tomcat-6.0.37_9300/temp org.apache.catalina.startup.Bootstrap stop 后报错。1
对于 catalina.sh 里面的就是这一句:
echo “0000002”
“$_RUNJAVA” $LOGGING_MANAGER $JAVA_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 “$@” stop
echo “0000003”
3.3.2 再次等 5 分钟,再 bash - x 看执行详细过程:
[root@ptbin]# bash -x catalina.sh stop
+ JAVA_OPTS=’-Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+PrintGCDetails -Xloggc:/logs/gc/9300.gc’
+ CATALINA_PID=/var/tomcat/9300.pid
+ cygwin=false
+ os400=false
+ darwin=false
+ case “`uname`” in
++ uname
+ PRG=catalina.sh
+ ‘[‘ -h catalina.sh ‘]’
++ dirname catalina.sh
+ PRGDIR=.
+ ‘[‘ -z ” ‘]’
++ cd ./..
++ pwd
+ CATALINA_HOME=/usr/local/app/apache-tomcat-6.0.37_9300
+ ‘[‘ -z ” ‘]’
+ CATALINA_BASE=/usr/local/app/apache-tomcat-6.0.37_9300
+ CLASSPATH=
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setenv.sh ‘]’
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setenv.sh ‘]’
+ false
+ false
+ false
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh ‘]’
+ BASEDIR=/usr/local/app/apache-tomcat-6.0.37_9300
+ . /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh
++ ‘[‘ -z /usr/lib/jvm/java -a -z ” ‘]’
++ ‘[‘ -z /usr/lib/jvm/java -a stop = debug ‘]’
++ ‘[‘ -z ” ‘]’
++ JRE_HOME=/usr/lib/jvm/java
++ ‘[‘ stop = debug ‘]’
++ ‘[‘ -z /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
++ ‘[‘ ‘!’ -x /usr/local/app/apache-tomcat-6.0.37_9300/bin/setclasspath.sh ‘]’
++ ‘[‘ -z ” ‘]’
++ JAVA_ENDORSED_DIRS=/usr/local/app/apache-tomcat-6.0.37_9300/endorsed
++ JIKESPATH=
+++ uname -s
++ ‘[‘ Linux = Darwin ‘]’
++ _RUNJAVA=/usr/lib/jvm/java/bin/java
++ ‘[‘ false ‘!=’ true ‘]’
++ _RUNJDB=/usr/lib/jvm/java/bin/jdb
+ ‘[‘ -z /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
+ ‘[‘ ‘!’ -z ” ‘]’
+ ‘[‘ /usr/local/app/apache-tomcat-6.0.37_9300 ‘!=’ /usr/local/app/apache-tomcat-6.0.37_9300 ‘]’
+ CLASSPATH=/usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar
+ ‘[‘ -z ” ‘]’
+ CATALINA_OUT=/usr/local/app/apache-tomcat-6.0.37_9300/logs/catalina.out
+ ‘[‘ -z ” ‘]’
+ CATALINA_TMPDIR=/usr/local/app/apache-tomcat-6.0.37_9300/temp
+ have_tty=0
++ tty
+ ‘[‘ /dev/pts/1 ‘!=’ ‘not a tty’ ‘]’
+ have_tty=1
+ false
+ ‘[‘ -z ” ‘]’
+ ‘[‘ -r /usr/local/app/apache-tomcat-6.0.37_9300/conf/logging.properties ‘]’
+ LOGGING_CONFIG=-Djava.util.logging.config.file=/usr/local/app/apache-tomcat-6.0.37_9300/conf/logging.properties
+ ‘[‘ -z ” ‘]’
+ LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
+ ‘[‘ 1 -eq 1 ‘]’
+ echo ‘Using CATALINA_BASE: /usr/local/app/apache-tomcat-6.0.37_9300’
Using CATALINA_BASE: /usr/local/app/apache-tomcat-6.0.37_9300
+ echo ‘Using CATALINA_HOME: /usr/local/app/apache-tomcat-6.0.37_9300’
Using CATALINA_HOME: /usr/local/app/apache-tomcat-6.0.37_9300
+ echo ‘Using CATALINA_TMPDIR: /usr/local/app/apache-tomcat-6.0.37_9300/temp’
Using CATALINA_TMPDIR: /usr/local/app/apache-tomcat-6.0.37_9300/temp
+ ‘[‘ stop = debug ‘]’
+ echo ‘Using JRE_HOME: /usr/lib/jvm/java’
Using JRE_HOME: /usr/lib/jvm/java
+ echo ‘Using CLASSPATH: /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar’
Using CLASSPATH: /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ echo ‘Using CATALINA_PID: /var/tomcat/9300.pid’
Using CATALINA_PID: /var/tomcat/9300.pid
+ ‘[‘ stop = jpda ‘]’
+ ‘[‘ stop = debug ‘]’
+ ‘[‘ stop = run ‘]’
+ ‘[‘ stop = start ‘]’
+ ‘[‘ stop = stop ‘]’
+ shift
+ SLEEP=5
+ ‘[‘ ‘!’ -z ” ‘]’
+ FORCE=0
+ ‘[‘ ” = -force ‘]’
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ ‘[‘ -f /var/tomcat/9300.pid ‘]’
+ ‘[‘ -s /var/tomcat/9300.pid ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 0 -gt 0 ‘]’
+ echo 0000002
0000002
+ /usr/lib/jvm/java/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+PrintGCDetails -Xloggc:/logs/gc/9300.gc -Djava.endorsed.dirs=/usr/local/app/apache-tomcat-6.0.37_9300/endorsed -classpath /usr/local/app/apache-tomcat-6.0.37_9300/bin/bootstrap.jar -Dcatalina.base=/usr/local/app/apache-tomcat-6.0.37_9300 -Dcatalina.home=/usr/local/app/apache-tomcat-6.0.37_9300 -Djava.io.tmpdir=/usr/local/app/apache-tomcat-6.0.37_9300/temp org.apache.catalina.startup.Bootstrap stop
+ echo 0000003
0000003
+ ‘[‘ ‘!’ -z /var/tomcat/9300.pid ‘]’
+ echo CATALINA_PID:/var/tomcat/9300.pid
CATALINA_PID:/var/tomcat/9300.pid
+ ‘[‘ -f /var/tomcat/9300.pid ‘]’
+ ‘[‘ 5 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 5 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 5 -eq 0 ‘]’
++ expr 5 – 1
+ SLEEP=4
+ ‘[‘ 4 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 4 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 4 -eq 0 ‘]’
++ expr 4 – 1
+ SLEEP=3
+ ‘[‘ 3 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 3 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 3 -eq 0 ‘]’
++ expr 3 – 1
+ SLEEP=2
+ ‘[‘ 2 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 0 -gt 0 ‘]’
+ ‘[‘ 2 -gt 0 ‘]’
+ sleep 1
+ ‘[‘ 2 -eq 0 ‘]’
++ expr 2 – 1
+ SLEEP=1
+ ‘[‘ 1 -ge 0 ‘]’
++ cat /var/tomcat/9300.pid
+ kill -0 23685
+ ‘[‘ 1 -gt 0 ‘]’
+ rm -f /var/tomcat/9300.pid
+ ‘[‘ 0 ‘!=’ 0 ‘]’
+ break
+ ‘[‘ 0 -eq 1 ‘]’1
对比下 3.3.1 代码报错的代码以及 bash 执行结果命令,
“$_RUNJAVA” $LOGGING_MANAGER $JAVA_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 “$@” stop
的执行结果都是一样的,没有差别,所以就是说问题在于执行 catalina.sh stop 的时机问题,那么这个时机不对,就会造成 stop 失败。在经过几次测试后,发现在以下几种情况会造成 tomcat 类似的错误:
(1)是由于 tomcat 还没有启动完成的时候,你去执行 shutdown.sh 会报错,这个时候不允许 stop,会报错。
(2)当 tomcat 上的 web 工程正在执行一项任务的时候,tomcat 会等待不允许你 stop 也会保错的。
(3)其它未知原因。
4,解决办法
4.1 加上 -force 参数,再启动后关闭,就不会报错
[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
[root@ptbin]#
4.2 等待一会再 shutdown.sh 等 tomcat 里面 web 工程执行完的时候,没有新请求了,shutdown 就 ok 了。
[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
[root@ptbin]#
RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目 http://www.linuxidc.com/Linux/2015-02/113528.htm
Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签) http://www.linuxidc.com/Linux/2015-02/113062.htm
Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm
Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm
CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm
CentOS 6.5 下安装 Tomcat http://www.linuxidc.com/Linux/2015-01/111415.htm
Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-06/118883.htm