#下面的代码紧接着stop的正常停止代码下
if [ ! -z "$CATALINA_PID" ]; then
#如果$CATALINA_PID不为空
if [ -f "$CATALINA_PID" ]; then
#而且还是普通文件
while [ $SLEEP -ge 0 ]; do
#而且$SLEEP还大于0
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
#则测试下tomcat能不能被关闭
if [ $? -gt 0 ]; then
#剩下的参数还有,则清空$CATALINA_PID
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
else
echo "Tomcat stopped but the PID file could not be removed or cleared."
fi
fi
break
fi
if [ $SLEEP -gt 0 ]; then
sleep 1
fi
if [ $SLEEP -eq 0 ]; then
if [ $FORCE -eq 0 ]; then
echo "Tomcat did not stop in time. PID file was not removed."
fi
fi
SLEEP=`expr $SLEEP - 1 `
done
fi
fi
#上段语句主要是判断tomcat是否被关闭
#核心语句还是:kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
#while语句做sleep使用,用于清空$CATALINA_PID
#但是[ ! -z "$CATALINA_PID" ]导致这段语句什么都没做啊!!!!!第五处:
#这段代码就是涉及-force的核心代码了
if [ $FORCE -eq 1 ]; then
if [ -z "$CATALINA_PID" ]; then
echo "Kill failed: \$CATALINA_PID not set"
else
if [ -f "$CATALINA_PID" ]; then
PID=`cat "$CATALINA_PID"`
echo "Killing Tomcat with the PID: $PID"
kill -9 $PID
#强制执行的核心命令
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
echo "Tomcat was killed but the PID file could not be removed."
fi
fi
fi
fi
#从中就可以看出,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的值,那么看结果:
+ FORCE=1
+ '[' '!' -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" ]; then
if [ -s "$CATALINA_PID" ]; then
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
#kill -0 pid 不发送任何信号,但是系统会进行错误检查。
if [ $? -gt 0 ]; then
echo "PID file found but no matching process was found. Stop aborted."
exit 1
fi
else
echo "PID file is empty and has been ignored."
fi
else
echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
exit 1
#$CATALINA_PID不是个文件,所以不好意思,我退出了....原来$CATALINA_PID还必须是个文件,那么我们可不可以建这么一个文件呢?
在catalina.sh脚本的代码前,加入以下语句: