Java中有處理收到的SIGTERM的方法嗎?如何處理SIGTERM
回答
是的,你可以用Runtime.addShutdownHook()
註冊關機鉤。
你可以添加一個shutdown hook做任何清理。
像這樣:
public class myjava{
public static void main(String[] args){
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Inside Add Shutdown Hook");
}
});
System.out.println("Shut Down Hook Attached.");
System.out.println(5/0); //Operating system sends SIGFPE to the JVM
//the JVM catches it and constructs a
//ArithmeticException class, and since you
//don't catch this with a try/catch, dumps
//it to screen and terminates. The shutdown
//hook is triggered, doing final cleanup.
}
}
然後運行它:
[email protected]:~$ javac myjava.java
[email protected]:~$ java myjava
Shut Down Hook Attached.
Exception in thread "main" java.lang.ArithmeticException:/by zero
at myjava.main(myjava.java:11)
Inside Add Shutdown Hook
另一種方式來處理信號Java是通過sun.misc.signal包。瞭解如何使用它,請參閱http://www.ibm.com/developerworks/java/library/i-signalhandling/。
注意: sun。*軟件包中的功能也意味着它可能在所有操作系統中都不可移植/行爲相同。但你可能想嘗試一下。
但事實上,它也是一樣的嗎? – 2010-06-07 09:39:18
不是 - 不完全是。我提到的信號處理API可用於處理Java應用程序中的大部分Native OS信號(陷阱,內聯,終止等)。 另一方面,ShutdownHook只有在JVM最終終止時(在收到終止信號後)纔會被調用。 – arcamax 2010-06-14 08:41:11
@arcamax鏈接似乎被破壞 – GreenGiant 2013-10-23 21:56:36
- 1. Java:如何僅處理SIGTERM?
- 2. wxPython處理SIGTERM/SIGINT
- 3. 在OSx中處理sigterm
- 4. SIGTERM處理程序調用多次
- 5. Python:什麼是SIGTERM的默認處理?
- 6. LaunchDaemon處理系統關閉 - 無SIGTERM?
- 7. 如何測試RSpec中的信號處理,特別是處理SIGTERM?
- 8. 如何在Java中優雅地處理SIGTERM信號?
- 9. 我該如何處理中斷(即SIGTERM或SIGINT)
- 10. 如何擺脫SIGTERM錯誤
- 11. Python:覆蓋默認的SIGTERM處理程序
- 12. Java進程卡在SIGTERM處理程序中刪除文件
- 13. 朱莉婭0.4.7:如何捕捉SIGTERM
- 14. 如何在Windows中提升SIGTERM
- 15. SIGTERM + Rails + mysql?
- 16. 收到SIGTERM
- 17. pcntl_wait不是SIGTERM
- 18. HTTP ::上未SIGTERM
- 19. Amazon Elastic MapReduce - SIGTERM
- 20. 收到SIGTERM
- 21. SIGTERM和SIGKILL
- 22. 如何處理
- 23. 如何處理
- 24. 如何處理
- 25. 如何處理
- 26. 如何處理
- 27. 如何處理
- 28. 如何處理
- 29. 在關閉Linux時關閉SIGTERM處理程序的超時配置位置
- 30. REST API:如何處理處理邏輯
博客條目是404 – luckydonald 2017-01-19 12:44:05