I am trying to create a console application which hangs up in a way that pressing CTRL + BREAK or sending a SIGTERM signal to the process doesn't terminate it [i.e. it keeps on hanging, without closing]. I want to test that it keeps on going with the following Java code:
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");
}
}
So far, I have managed to find the following information: How Can I Make A Command Prompt Hang? , though it doesn't stop SIGTERM, just SIGINT. I have also tried using Shutdown Hooks in a java -jar executable file, which also give me control over SIGINT,but not over SIGTERM. I want the program to keep running when given SIGTERM,so that I test the destroy function. I have also written a program in Go which does a similar thing,with the exception that it registers CTRL+ BREAK as an interrupt,for some reason[I am not sure why, but it still doesn't handle SIGTERM signals from the java code]:
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
}
NOTE: I still want to be able to close the application with a SIGKILL signal, or the red X in the window :) Thank you for any ideas :)