tags:

views:

2135

answers:

5

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:

+3  A: 

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.

sblundy
Checking the parent's status periodically can be reliable if you're polling to see if the child's PPID becomes 1, since orphaned processes get a PPID of the init process. (PID=1)
Aaron
A: 

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

Killing a process normally doesn't allow it any sort of clean up. Which is exactly the problem.
sblundy
That depends on how you kill a process and under what OS. SIGTERM on Unixes allows the program to do cleanup, SIGKILL does not.
R. Bemrose
+3  A: 

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.addShutdownHook(closeChildThread);
Greg Case
+1  A: 

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.

dmckee
+1  A: 

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.

jmucchiello