Java ProcessBuilder process.destroy() not killing child processes in winXP

java

I have a java app that uses ProcessBuilder to prepare an operating system command and gives me a Process object. (The actual os command is rsync over ssh using cygwin).

This is working well in Windows, however if I want to stop the process using process.destroy() it will not kill the child ssh and rsync processes…..
I have to manually kill them using the windows task manager..

Is it possible to get the OutputStream of the process and send a ctrl-c somehow before I call destroy();?

If anyone has any ideas on a workaround it would be great.
Thanks,
D

Best Answer

I also think that emulating Ctrl-C in order to kill ssh entirely is problematic.

What I would do, is one of the following approaches. Either use windows commands to find out who are ssh's sons (which is a little bit problematic, since you need to know your current pid in order to recieve your own children-processes). I believe pstools of sysinternals is a good command-line tool that should enable you to track orphan processes. See this example for controlling windows processes either with taskList.exe (which can give you its output in CSV format BTW) or by executing a special VBScript.

The second approach is using a java library such as winp to execute and control the ssh process. I believe you'd be able to list all its children and forcibly kill them if sending the correct message would not suffice. This would be my preferred approach. Please note that the killRecursively method does exactly what you want.

Please note that those approaches should not render your application windows only. You can encapsulate those in a class that would run differently on Windows and linux machines.

Please note I didn't try to gain a fine-grained control on windows processes with, so I'm not sure how mature would those solutions I found are.

Related Topic