Terminate child processes spawned by ProcessBuilder on * nix

I am executing a shell pipeline from a java program - it will be something like

ProcessBuilder builder = new ProcessBuilder(
                         "sh", "-c", "program1 | program2 | program3");
builder.start();

In some cases, this device may be discontinued. but

process.destroy();

Destroy the sh command. The commands in pipline will be orphaned and accepted by the init process.

Is there a way to easily terminate all these child processes - or execute a pipeline, as shown above, in such a way as to simplify their completion. Changing program 1/2/3 is not possible. Portability outside of Linux is not a problem.

+3
source share
3 answers

I can think of it in two ways:

  • You can run pkill program1 program2 program3

  • , bash, , , STOP.

+1

, , java.

( program1, program2 ... one program3: D) .

j.l.Process destroy() .

0

Create a wrapper program in C that (a) runs the rest of the channel, and (b) processes some signals, killing all the participants in the pipe before calling exit (2) itself.

0
source

Source: https://habr.com/ru/post/1721396/


All Articles