Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate child processes spawned by a ProcessBuilder on *nix

Tags:

java

I'm executing a shell pipeline from a java program - it'll be something like

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

In some cases this unit might need to be terminated. However

process.destroy();

Will only destroy the "sh" command. The commands in the pipline will be orphaned and adopted by the init process.

Is there any way to easily terminate all these child processes - or execute a pipeline like the above in a way that makes it easier to terminate them . Altering progam 1/2/3 can't be done. Portability beyond linux is not a issue.

like image 529
Anonym Avatar asked Dec 06 '25 14:12

Anonym


1 Answers

There's two ways I can think to do this:

  1. You could run a pkill program1 program2 program3

  2. You could write a intermediate program which launches the whole bash command line, this intermediate program would install a signal handler which kills it's own children when it gets a STOP signal.

like image 162
Benj Avatar answered Dec 08 '25 02:12

Benj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!