简体   繁体   English

Linux Shutdown和Java Shutdown Hook

[英]Linux Shutdown and Java Shutdown Hook

当我在后台运行Java进程并关闭计算机(ArchLinux)时,计算机是否会等待几秒钟以终止Java中的shutdown-hook?

In case you want to do something on system shutdown, you need to add a shutdown-hook (assuming you already do this) : 如果你想在系统关闭时做一些事情,你需要添加一个shutdown-hook(假设你已经这样做了):

Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() {
    @Override
    public void run() {
        // This method will be executed and Virtual Machine will close when this method will return
    }
}));

The system will NEVER await a full termination if it took too much time 如果花费太多时间 ,系统将永远不会等待完全终止

From the doc Runtime#addShutdownHook 来自doc Runtime#addShutdownHook

When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. 当虚拟机因用户注销或系统关闭而终止时,底层操作系统可能只允许一段固定的时间来关闭和退出。 It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook. 因此,不建议尝试任何用户交互或在关闭钩子中执行长时间运行的计算。

You can wrap your java program as a service and it will started and shut down with OS. 您可以将Java程序包装为服务,它将启动并关闭操作系统。 One of the easiest way to do so, is using spring boot framework . 最简单的方法之一是使用spring boot框架

A shutdown hook will be called when a call to close the JVM is made. 当关闭JVM的调用被调用时,将调用关闭钩子。 However, there is no guarantee that the hook will be called. 但是,无法保证将调用挂钩。 The hook might not/won't run: 钩子可能不会/不会运行:

  • If the JVM crashes from an internal error. 如果JVM因内部错误而崩溃。
  • If SIGKILL is signaled, or kill -9 in linux and TerminateProcess in windows. 如果发出SIGKILL信号,或者在linux中杀死-9,在windows中杀死TerminateProcess。
  • The Runtime.halt() will also prevent a hook from begin run. Runtime.halt()还将阻止钩子开始运行。
  • The computer power is pulled (at the plug). 拉动计算机电源(插头处)。
  • Your OS crashes. 你的操作系统崩溃了。

When the user turns off the computer (not by pulling the plug), the computer sends a shutdown command to the JVM which will execute the shutdown hooks, although if the power is pulled the computer won't have any time to do anything, let alone issue a command to the JVM. 当用户关闭计算机时(而不是通过拉动插头),计算机会向JVM发送一个关闭命令,该命令将执行关闭挂钩,但是如果拔掉电源,计算机将没有时间做任何事情,让单独向JVM发出命令。

I remember reading somewhere (but i couldn't find the link again) - it said that a shutdown hook shouldn't take to much time to execute or windows might kill it when the system wants shutdown. 我记得在某处读过(但我再也找不到链接) - 它说一个关闭钩子不应该花费很多时间来执行,或者当系统想要关闭时windows可能会杀死它。 This indicates that the shutdown hooks will be run if you decide to turn off the system though the OS. 这表示如果您决定通过操作系统关闭系统,将运行关闭挂钩。

Tl;dr: It depends, pulling the power directly or if the OS crashes, they won't be run. Tl; dr:这取决于直接拉动电源,或者如果操作系统崩溃,它们将无法运行。 A normal OS shutdown will run the hooks, although it's possible the OS might close them before they finish if they're taking too long. 正常的操作系统关闭将运行挂钩,但操作系统可能会在完成之前关闭它们,如果它们花费的时间太长。


This is from the javadoc. 这是来自javadoc。

When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. 当虚拟机因用户注销或系统关闭而终止时,底层操作系统可能只允许一段固定的时间来关闭和退出。 It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook. 因此,不建议尝试任何用户交互或在关闭钩子中执行长时间运行的计算。

This states that the hooks will be run when the OS wants to shutdown, but it's advised that they don't take long or they might get killed. 这表明当操作系统想要关闭时会挂起钩子,但是建议它们不会花很长时间或者它们可能会被杀死。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM