我正在創建TFTP服務器,並使用無限循環保持它運行。當我嘗試發送kill信號時Java進程的退出鉤子
/bin/kill -s SIGINT <pid>
我希望Java進程關閉TFTP服務器並退出。
class TFTP {
public void startTFTP(String str, String str1){
try {
//Start TFTP server
// Both of below are not working.
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
//Shutdown tftp server
System.out.println("Exiting");
System.exit(0);
}
});
SignalHandler handler = new SignalHandler() {
public void handle(Signal signal) {
System.exit(0);
}
};
Signal.handle(new Signal("INT"), handler);
Signal.handle(new Signal("TERM"), handler);
while(true){
// Infinite loop to keep process running.
}
}
} catch (Exception e) {
}
}
public static void main(String args[]){
TFTP tftp = new TFTP();
tftp.startTFTP(args[0],args[1]);
}
}
我從腳本殺死進程,並呼籲
/bin/kill -s SIGINT <pid>
不殺出於某些原因,Java進程。我以爲SIGINT是作爲
的一部分處理的Signal.handle(new Signal("INT"), handler);
我在這裏錯過了什麼?
問候
Dheeraj喬希
你見過http://stackoverflow.com/questions/2541475/capture-sigint-in-java? – 2012-04-03 15:22:57
是的,我沒有谷歌搜索和搜索舊的StackOverFlow線程。但殺-2也不適合我。所以我想知道我想我做了一些基本的錯誤。 – 2012-04-03 15:28:54