Tomcat优雅关机 [英] Tomcat graceful shutdown

查看:62
本文介绍了Tomcat优雅关机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tomcat 8.x.x 并使用 startup.sh 启动 tomcat.我使用 JPS 命令验证了 tomcat 已启动并正在运行.我正在使用 catalina.sh stop 100 停止 Tomcat.根据 catalina shell 脚本,它将等待 100 秒以终止 tomcat 进程.但是tomcat进程立即终止,通过JPS命令验证.

I am using Tomcat 8.x.x and starting the tomcat using startup.sh. I verified that tomcat is up and running using JPS command. I am stopping the Tomcat using catalina.sh stop 100. As per catalina shell scripts says it will wait for 100 seconds to terminate the tomcat process. But tomcat process terminated immediately, verified by JPS command.

为什么正常关机不起作用?

Why Graceful shutdown is not working ?

如何在Tomcat中实现优雅关机?

How to achieve the Graceful shutdown in Tomcat ?

推荐答案

100 秒是脚本在发出 kill 信号后等待进程停止的时间,然后用 kill -9<更强力地终止它/代码>.仅仅因为该过程快速停止,并不意味着它不是完​​全关闭.

The 100 seconds is how long the script will wait for the process to stop after issuing the kill signal before terminating it more forcefully with kill -9. Just because the process stops quickly, doesn't mean it wasn't a clean shutdown.

来自 catalina.sh 帮助部分:

From the catalina.sh help section:

echo "  stop              Stop Catalina, waiting up to 5 seconds for the process to end"
echo "  stop n            Stop Catalina, waiting up to n seconds for the process to end"
echo "  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running"
echo "  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running"

注意措辞 waiting up to n seconds ... 进程立即发出信号,脚本等待长达 n 秒让 PID 在它之前消失发送 kill -9 信号.请注意,catalina.sh 脚本每次会休眠 1 秒,但仅在它发送初始关闭信号之后.

Notice the verbiage waiting up to n seconds ... the process is signaled right away, and the script waits up to n seconds for the PID to disappear before it sends the kill -9 signal. Note that the catalina.sh script does sleep for 1 second at a time, but only after it has sent the initial shutdown signal.

以下是脚本中的相关行,向您展示了正在发生的事情:

Here are the relevant lines from the script that show you exactly what's happening:

https://github.com/apache/tomcat/blob/a8c62dd061d4cf937c3bdec615121696916434eb/bin/catalina.sh#L525-L600

一般来说:

  • 向进程发送一个 kill -15,这是一个正常、优雅的关闭.现在由 JVM 开始其终止序列.
  • 向进程发送一个 kill -0 以检查它是否还活着.如果还活着,则休眠 1 秒并再次检查.重复此操作,直到 a) 进程不再活动,或 b) 延迟参数(在您的情况下为 100 秒)已过去.
  • 如果此时进程仍然活动,并且您提供了 -force 参数,则脚本最后一次尝试发送 kill -9 给JVM 的信号.该信号实际上无法由 JVM 处理,操作系统本身会强制立即关闭进程(即非正常关闭).
  • Send a kill -15 to the process, which is a normal, graceful shutdown. It is now up to the JVM to start its termination sequence.
  • Send a kill -0 to the process to check if it is alive or not. If it's alive, sleep for 1 second and check again. Repeat this until either a) the process is no longer alive, or b) the delay parameter (in your case, 100 seconds) has elapsed.
  • If the process is still alive at this point, and you have provided the -force argument, the script tries one last time to send a kill -9 signal to the JVM. This signal cannot actually be handled by the JVM, and the OS itself forces an immediate shutdown of the process (i.e. a not graceful shutdown).

请注意,所有这一切都是立即发生的,没有任何延迟.如果你想在尝试上面的关闭序列之前有一些人为的暂停,你应该在调用 catalina.sh 之前使用 sleep :

Notice all of this happened right away, without any delay. If you want to have some artificial pause before attempting the shutdown sequence above, you should use sleep before calling catalina.sh:

sleep 100 && catalina.sh stop

这篇关于Tomcat优雅关机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆