如何在Java中优雅地处理SIGKILL信号 [英] How to gracefully handle the SIGKILL signal in Java

查看:260
本文介绍了如何在Java中优雅地处理SIGKILL信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当程序收到终止信号时,如何处理清理?

How do you handle clean up when the program receives a kill signal?

例如,我连接的应用程序需要任何第三方应用程序(我的应用程序)发送完成退出时的命令。当我的应用程序被 kill -9 销毁时,发送完成命令的最佳发音是什么?

For instance, there is an application I connect to that wants any third party app (my app) to send a finish command when logging out. What is the best say to send that finish command when my app has been destroyed with a kill -9?

编辑1:无法捕获kill -9。谢谢你们纠正我。

edit 1: kill -9 cannot be captured. Thank you guys for correcting me.

编辑2:我猜这种情况是当一个人调用kill时这与ctrl-c相同

edit 2: I guess this case would be when the one calls just kill which is the same as ctrl-c

推荐答案

处理其他而非 kill -9 的任何方法都是注册关闭钩子。如果您可以使用( SIGTERM kill -15 ,则关闭钩子将起作用。 ( SIGINT kill -2 DOES 导致程序正常退出并运行关闭挂钩。

The way to handle this for anything other than kill -9 would be to register a shutdown hook. If you can use (SIGTERM) kill -15 the shutdown hook will work. (SIGINT) kill -2 DOES cause the program to gracefully exit and run the shutdown hooks.


注册一个新的虚拟机
shutdown hook。

Registers a new virtual-machine shutdown hook.

Java虚拟机关闭
对两种事件的响应:

The Java virtual machine shuts down in response to two kinds of events:

* The program exits normally, when the last non-daemon thread exits or

当调用退出(相当于
System.exit)方法时,或

when the exit (equivalently, System.exit) method is invoked, or

* The virtual machine is terminated in response to a user

中断,例如输入^ C或
系统范围的事件,例如用户注销
或系统关闭。

interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

我在OSX 10.6.3和 kill -9 上尝试了以下测试程序 NOT 运行关闭钩子,不认为会。在 kill -15 上, DOES 每次都运行关闭钩子。

I tried the following test program on OSX 10.6.3 and on kill -9 it did NOT run the shutdown hook, didn't think it would. On a kill -15 it DOES run the shutdown hook every time.

public class TestShutdownHook
{
    public static void main(final String[] args) throws InterruptedException
    {
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                System.out.println("Shutdown hook ran!");
            }
        });

        while (true)
        {
            Thread.sleep(1000);
        }
    }
}

没有任何办法在任何程序中真正优雅地处理 kill -9

There isn't any way to really gracefully handle a kill -9 in any program.


在极少数情况下虚拟的
机器可能会中止,也就是说,停止
运行而不会干净地关闭。
当外部终止虚拟机
时会发生这种情况,例如Unix上带有SIGKILL信号的
或Microsoft
Windows上的
TerminateProcess调用。

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows.

处理 kill -9 的唯一真正选择是让另一个观察程序监视您的主程序要么离开或使用包装脚本。您可以使用shell脚本来执行此操作,该脚本轮询 ps 命令,在列表中查找您的程序,并在消失后立即采取相应措施。

The only real option to handle a kill -9 is to have another watcher program watch for your main program to go away or use a wrapper script. You could do with this with a shell script that polled the ps command looking for your program in the list and act accordingly when it disappeared.

#!/usr/bin/env bash

java TestShutdownHook
wait
# notify your other app that you quit
echo "TestShutdownHook quit"

这篇关于如何在Java中优雅地处理SIGKILL信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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