How to kill subprocesses of a Java process?

I am creating a P1 process using Process P1= Runtime.exec(...) . My process P1 creates another process, for example P2, P3 ....

Then I want to kill the process P1 and all the processes created by P1, that is, P2, P3 ...

P1.destroy() kills only P1, not its subprocesses.

I also found Googled and found a Java error: http://bugs.sun.com/view_bug.do?bug_id=4770092

Does anyone have any ideas on how to do this?

+4
source share
5 answers

Yes, this is a mistake, but if you are reading the assessment, the main problem is that it is almost impossible to implement “kill all small children” on Windows.

The answer is that P1 must be responsible for executing its own procedure.

+3
source

Java does not provide reliable information about the grandchildren of the process. If your child process starts another process, then a child process is required to manage them.

I would suggest either

  • Refactoring your project so that your parent creates / controls all child processes or
  • Using operating system commands to kill processes or
  • Using another control mechanism, such as the Inter-Process Communication form (there are many Java libraries for this).

Support to @Giacomo for offering IPC in front of me.

+1
source

Are you writing code for other processes or is it something you cannot change?

If possible, I would consider modifying them so that they receive some kind of message (even through standard streams), so that they beautifully stop on request, completing the children, if any, on their own.

I do not think that the "process of destruction" is pure.

0
source

if this is a mistake, as you say, you should keep track of the pf process tree of the child process and kill the entire child process from the tree, when you want to kill the parent process you need to use the data structure tree for this, if you have only a couple of processes than the use list

0
source

Since Runtime.exec () returns an instance of Process , you can use some array to store your link and kill them later Process.destroy () .

-1
source

All Articles