[英]Raspberry Pi Java Shutdown hook
I have a Raspberry Pi running Java 1.8.0 and a file called test.jar. 我有一个运行Java 1.8.0的Raspberry Pi和一个名为test.jar的文件。 When I run the code and then stop the program with Ctrl+Z the Shutdown hook does not run but when I run the code on windows and stop it, the shutdown hook will work.
当我运行代码,然后使用Ctrl + Z停止程序时,关机挂钩不会运行,但是当我在Windows上运行代码并将其停止时,关机挂钩将起作用。
How can I fix this, Thanks 我该如何解决这个问题,谢谢
public class Test
{
public static void main(String[] args)
{
Runtime.getRuntime().addShutdownHook(new Thread() //Add shutdown code
{
public void run()
{
System.out.println("Shutdown");
}
});
while(true) { }
}
}
In a linux terminal, ctrl-z sends a SIGSTOP
to the foreground process. 在Linux终端中,ctrl-z将
SIGSTOP
发送到前台进程。 This is one of two signals (the other being SIGKILL
) that you cannot handle in your process. 这是您在处理过程中无法处理的两个信号之一(另一个是
SIGKILL
)。 That means that java has no way to run any code in response to the signal. 这意味着java无法响应该信号运行任何代码。
However, SIGSTOP
doesn't end the process anyway, it simply pauses it. 但是,
SIGSTOP
无论如何都不会结束该过程,只是将其暂停。 You can continue it by sending a SIGCONT
signal, which can be achieved in your shell by using the fg
command. 您可以通过发送
SIGCONT
信号来继续操作,这可以在您的Shell中使用fg
命令来实现。
Try using ctrl-c
instead to end your program, and it ought to work (since that will send a SIGINT
instead, which can be handled). 尝试使用
ctrl-c
结束程序,它应该可以工作(因为它将发送SIGINT
,可以处理)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.