banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

Tomcat进程无法正常stop问题

CentOS 使用 Catalina.sh 来管理 Tomcat 运行时,Catalina.sh run 之后产生的 tomcat 进程会无法使用 Catalina.sh stop -force 关闭。如果 Catalina.sh 的默认内容来配置 tomcat 服务,/etc/init.d/tomcat stop 也将会失效

查看 Catalina.sh 文件后找到以下代码,原来需要 stop 的话,需要 CATALINA_PID 文件配合。 Catalina.sh 468 行

if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
if [ -s "$CATALINA_PID" ]; then
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
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
fi
fi

CATALINA_PID 的说明内容 Catalina.sh 89 行

# CATALINA_PID (Optional) Path of the file which should contains the pid

of the catalina startup java process, when start (fork) is#

used#

解决办法就是,在配置 Catalina.sh 的环境变量时添加 CATALINA_PID 变量

#创建 pid 文件
touch /usr/local/tomcat/logs/tomcat.pid

在 /etc/init.d/tomcat 头部增加环境变量配置#

sed -i '1a\. /etc/init.d/functions' /etc/init.d/tomcat
sed -i '2a\:' /etc/init.d/tomcat
sed -i '3a\JAVA_HOME=/usr/local/jdk' /etc/init.d/tomcat
sed -i '4a\CATALINA_BASE=/usr/local/tomcat' /etc/init.d/tomcat
sed -i '5a\CATALINA_HOME=/usr/local/tomcat' /etc/init.d/tomcat

添加 PID 文件位置#

sed -i ‘6a\CATALINA_PID=/usr/local/tomcat/logs/tomcat.pid' /etc/init.d/tomcat

最后 使用 Catalina.sh 方式单独运行 tomcat 时,建议在 $CATALINA_HOME/bin 下新建 setenv.sh 方式实现

参考 http://lovesoo.org/tomcat-process-fails-to-stop-problem-solving.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.