How can I cause a child process to exit when the parent does?

后端 未结 9 1911
一生所求
一生所求 2020-11-27 16:07

I am launching a child process with ProcessBuilder, and need the child process to exit if the parent process does. Under normal circumstances, my code is stopping the child

9条回答
  •  有刺的猬
    2020-11-27 16:33

    You can simply start a thread reading from System.in in the child process. If you are not writing to stdin of your child, nobody else will do and the thread will block "forever". But you will get an EOF (or an exception), if the parent process is killed or dies otherwise. So you can shutdown the child as well. (Child process started with java.lang.ProcessBuilder)

提交回复
热议问题