现在的位置: 首页 > 软件测试 > 测试工具 > 软件测试 > 正文

Tomcat进程无法正常stop问题解决

2017年01月19日 测试工具, 软件测试 ⁄ 共 2204字 ⁄ 字号 评论关闭 ⁄ 阅读 558 次

最近在CentOS上经常遇到Tomcat进程无法正常停止的问题(使用命令catalina.sh stop),后来查看帮助发现可以使用catalina.sh stop -force命令来强制停止Tomcat进程

而想要正常的使用stop -force命令,需要配置一个参数$CATALINA_PID,否则会报错Kill failed: $CATALINA_PID not set。

1. 使用命令catalina.sh --help查看帮助

可以看到 stop -force命令是先stop,如果5秒后进程还存在则直接 -KILL。

注意下面的Note提到,使用-force命令必须先定义变量$CATALINA_PID。


[[email protected] apache-tomcat]# ./bin/catalina.sh --help
Using CATALINA_BASE:   /home/apache-tomcat
Using CATALINA_HOME:   /home/apache-tomcat
Using CATALINA_TMPDIR: /home/apache-tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_91
Using CLASSPATH:       /home/apache-tomcat/bin/bootstrap.jar:/home/apache-tomcat/bin/tomcat-juli.jar
Using CATALINA_PID:    /home/apache-tomcat/bin/CATALINA_PID
Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

2. 查看catalina.sh文件寻求帮助

catalina.sh文件开头就说明了如何配置变量:不能在catalina.sh中设置,正确的做法是在$CATALINA_BASH/bin/setenv.sh脚本文件中配置。

重点看我们需要使用的CATALINA_PID参数,这是一个文件路径存放了Tomcat启动时PID,stop -force时会打开这个文件,获取pid,如果5秒后进程还在就直接KILL。

它还详细解释了其他变量的含义,在此不做赘述。

[[email protected] apache-tomcat]# vim bin/catalina.sh
# Environment Variable Prerequisites
#
#   Do not set the variables in this script. Instead put them into a script
#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
#   CATALINA_HOME   May point at your Catalina "build" directory.
#
#   ...

#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of the catalina startup java process, when start (fork) is
#                   used

3. 修改setenv.sh文件

编辑文件$CATALINA_HOME/bin/setenv.sh并增加x权限

#!/bin/bash
CATALINA_PID=$CATALINA_HOME/bin/CATALINA_PID

至此可以正常使用stop -force命令强制关闭Tomcat进程。

抱歉!评论已关闭.