Java Process.Destroy () does not kill the whole process

I have a Java process that runs a program using Runtime.getRuntime (). exec ("myBin")

this "myBin" can evolve several times to finish the job.

I have a stream of readers to read all the output of "myBin" and its children from the InputStream and ErrorStream of the Process object returned by exec ()

My question is: if it takes too much time to complete the task, I need to kill the process and wait for the reader stream to finish (the read stream will end if it reads EOF)

Now I found that even I use Process.Destroy (), I can only kill "myBin" from myself and all my children. Therefore, after the timeout, EOF did not reach, so the reader flow freezes until all child processes stop ...

Is there a way to execute the safty kill process and all children running Runtime.exec ()

I am in Linux, cross-platform is not in my mind.

+4
source share
1 answer

One way to achieve this would be to have a process that calls the plug in order to maintain a list of baby pids. You can implement a handler in mybin, which launches to kill "children" as well.

Another option is to use streams instead of using forks.

The main problem why this will not work is that when you call fork, it will create a new process that does not have a real dependency on the parent process.

+1
source

All Articles