I am trying to use a watchdog for my program, but it does not work if I used cmd.exe to launching it. Watchdog kills the process if the process is started natively (without cmd.exe), but it does nothing if the program is started with cmd.exe.
Working code example:
CommandLine cmd1 = CommandLine.parse("mysql");
ExecuteWatchdog watchdog = new ExecuteWatchdog(3 * 1000); // wait for 3 sec
Executor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
try {
executor.execute(cmd1);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("DONE!");
Changing the command to this, will block the thread forever ('mysql' waits for user input):
CommandLine cmd1 = CommandLine.parse("cmd /C start /wait cmd.exe /C 'mysql'");
Do you have any idea, how to solve this problem? The 'mysql' command should be run in a new cmd.exe window.
I solved this by extending ExecuteWatchdog; ie:
The real magic is destroying the descendants along with the root process.
I have a unit test showing that basically does this:
bash "script.sh" where script.sh is just a "sleep 5"
Before using ExecuteWatchdog, the ExecuteWatchdog would timeout the process with an exit code of 143, but only after 5 seconds. After replacing ExecuteWatchdog with DestroyDescendantsExecutorWatchDog with a 5ms timeout, the unit test almost immediately exits with the expected return code of 143.