一、问题背景

环境:linux,tomcat9
问题描述:tomcat执行shutdown.sh之后进程还存在
原因:项目中存在非守护线程Thread.currentThread().setDaemon(false);

二、问题解决

思路:

(一)、设置线程为守护线程Thread.currentThread().setDaemon(true);
(二)、记录tomcat进程pid,shutdown时强制关闭进程

下面介绍思路二的实现,即采用强制关闭进程的方法

1、进入tomcat的bin目录,使用vim catalina.sh命令编辑文件,使用./ 搜索PRGDIR=`dirname "$PRG"`然后回车快速定位到添加代码的位置,在该行代码下面添加如下内容后保存退出

# Get standard environment variables
PRGDIR=`dirname "$PRG"`
if [ -z "$CATALINA_PID" ]; then
 
      CATALINA_PID=$PRGDIR/CATALINA_PID
 
fi

2、在tomcat的bin目录下,使用vim shutdown.sh命令编辑文件,使用./ 搜索exec "$PRGDIR"/"$EXECUTABLE" stop快速定位到修改的地方,在stop后面添加-force参数,保存退出。如下图所示:

  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
fi

exec "$PRGDIR"/"$EXECUTABLE" stop -force "$@"

下面是验证过程,执行./startup.sh后 tomcat启动过程如下:

[root@local bin]$ ./startup.sh
Using CATALINA_BASE:   /opt/server/tomcat9
Using CATALINA_HOME:   /opt/server/tomcat9
Using CATALINA_TMPDIR: /opt/server/tomcat9/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_211/jre
Using CLASSPATH:       /opt/server/tomcat9/bin/bootstrap.jar:/opt/server/tomcat9/bin/tomcat-juli.jar
Using CATALINA_PID:    /opt/server/tomcat9/bin/CATALINA_PID

最后一行可以看到使用了CATALINA_PID,下面是执行./shutdown.sh命令后关闭tomcat的提示:

[root@local bin]$ ./shutdown.sh
Using CATALINA_BASE:  /opt/server/tomcat9
Using CATALINA_HOME:  /opt/server/tomcat9
Using CATALINA_TMPDIR:/opt/server/tomcat9/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_211/jre
Using CLASSPATH:      /opt/server/tomcat9/bin/bootstrap.jar:/usr/local/tomcat-9.0.34/bin/tomcat-juli.jar
Using CATALINA_PID:   /opt/server/tomcat9/bin/CATALINA_PID
Tomcat did not stop in time.
To aid diagnostics a thread dump has been written to standard out.
Killing Tomcat with the PID: 27932
The Tomcat process has been killed.

最后两行已经提示kill掉进程,使用ps -ef|grep tomcat命令查看,确实该tomcat进程已经不存在了,tomcat已经成功关闭。

Logo

更多推荐