开发者

Terminate child processes spawned by a ProcessBuilder on *nix

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开发者_如何学C - 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.


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.


Instead of running the pipeline in the shell, build the pipeline in java.

You'll need three process builders then (one for program1, one for program2, and ... one for program3 :D), and some threads to transfer the output from each process' output stream to the input stream of the next process.

This way you get j.l.Process instances for each of the childs, and can call destroy() on those.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜