我正在尝试创建一个控制台应用程序,它以按 CTRL + BREAK 或向进程发送 SIGTERM 信号不会终止它的方式挂起[即它一直挂起,而不关闭]。我想测试它是否继续使用以下 Java 代码:
public static void main(String[] args) throws IOException {
//Replace APPLICATION PATH HERE with path towards the executable file
Process process = Runtime.getRuntime().exec("APPLICATION PATH HERE");
// this should kill the process
process.destroy();
// if the process is alive, exitValue() will throw exception
try {
process.exitValue();
// the process is dead - hasn't survived kill
System.out.println("WRONG: process died");
} catch (IllegalThreadStateException e) {
// the process is still running
// the process is not dead and survived destroy()
System.out.println("OK: process hanged");
}
}
到目前为止,我已经设法找到以下信息: 如何使命令提示符挂起?,虽然它不会停止 SIGTERM,只是 SIGINT。我还尝试在 java -jar 可执行文件中使用 Shutdown Hooks,这也让我可以控制 SIGINT,但不能控制 SIGTERM。我希望程序在给定 SIGTERM 时继续运行,以便我测试破坏功能。我还在 Go 中编写了一个程序,它做了类似的事情,除了它将 CTRL+ BREAK 注册为中断,出于某种原因[我不知道为什么,但它仍然不处理来自 java 代码的 SIGTERM 信号] :
package main
import "fmt"
import "os"
import "os/signal"
import "syscall"
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-sigs
fmt.Println("TEST!!")
fmt.Println(sig)
done <- true
}()
fmt.Println("awaiting signal")
<-done
sigs2 := make(chan os.Signal, 1)
done2 := make(chan bool, 1)
signal.Notify(sigs2, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig2 := <-sigs2
fmt.Println("TEST!!")
fmt.Println(sig2)
done2 <- true
}()
fmt.Println("awaiting signal 2")
<-done2
}
注意:我仍然希望能够使用 SIGKILL 信号或窗口中的红色 X 关闭应用程序 :) 谢谢您的任何想法 :)