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

Viewed 58953

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 properly. However, if I cause the OS to kill the parent, the child will continue running.

Is there any way to "tie" the child process to the parent, such that it'll exit when the parent is killed?


Similar questions:

10 Answers

While you cannot protect against a hard abort (e.g. SIGKILL on Unix), you can protect against other signals that cause your parent process to shut down (e.g. SIGINT) and clean up your child process. You can accomplish this through use of shutdown hooks: see Runtime#addShutdownHook, as well as a related SO question here.

Your code might look something like this:

String[] command;
final Process childProcess = new ProcessBuilder(command).start();

Thread closeChildThread = new Thread() {
    public void run() {
        childProcess.destroy();
    }
};

Runtime.getRuntime().addShutdownHook(closeChildThread); 

There is no tie between a child process and its parent. They may know each others process ID, but there's no hard connection between them. What you're talking about a orphan process. And it's an OS level concern. Meaning any solution is probably platform dependent.

About the only thing I can think of is to have the child check its parents status periodically, exiting if the parent's shutdown. I don't think this would be all that reliable though.

As you've found the operating system allows you to get around this issue. Instead, create a resource shared by both processes. When the parent aborts the resource, the child reacts by shutting down. For example:

Create a thread with an server-side TCP/IP socket in accept mode on the parent on a random high number port. When the child starts, pass the port number as a parameter (environment variable, database entry, whatever). Have it create a thread and open that socket. The have the thread sit on the socket forever. If the connection ever drops, have the child exit.

or

Create a thread on parent that continually updates the update date on a file. (How often depends on how much granularity between kill and shutdown you need.) The child has a thread that monitors the update time of the same file. If it doesn't update after a specific interval, automatically shutdown.

For a single child processes you can manage this by inverting the child/parent relationship. See my answer to a later incarnation of this question.

Try to send a kill signal to the child process from the parent when the parent stops

Once we have the process id (PID) of the parent we can kill all the child process. We can use the windows taskkill command to kill any process that was started by the corresponding PID.

You can find the PID based on your use-case from the following post: https://stackoverflow.com/Questions/4750470/how-to-get-pid-of-process-ive-just-started-within-java-program

Once you have your PID all you need to do is to ask the OS to kill the process for you by sending the following command: "taskkill /F /T /PID P_id". Here replace the P_id with your process id. Here /T kills all the processes in the process tree of the corresponding PID.

You can use java.lang.Runtime.exec function to execute the above command.

PS: This solution will only work for Windows OS.

Related