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.
Best Answer
There's two ways I can think to do this:
You could run a pkill program1 program2 program3
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.