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

57,765

Solution 1

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.

Solution 2

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); 

Solution 3

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)

Solution 4

Provide a hacker way which is similar with the answer from @tomkri and also provide the demo code.

If your child process do not need use input stream, just redirect child process input stream to its parent process's input stream. Then add a thread in child to always read input stream and when this thread can not read anything from input stream, this child process exits. So the parent process exits -> parent's input stream does not exist -> child's input stream does not exist -> child process exits.

Here is the demo code all in Java.

Parent Process:

package process.parent_child;

import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;

public class ParentProc {

    public static void main(String[] args) {
        System.out.println("I'm parent.");

        String javaHome = System.getProperty("java.home");
        String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
        ProcessBuilder builder = new ProcessBuilder(javaBin, "process.parent_child.ChildProc");

        // Redirect subprocess's input stream to this parent process's input stream.
        builder.redirectInput(Redirect.INHERIT);
        // This is just for see the output of child process much more easily.
        builder.redirectOutput(Redirect.INHERIT);
        try {
            Process process = builder.start();
            Thread.sleep(5000);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Parent exits.");
    }
}

Child Process:

package process.parent_child;

import java.io.IOException;
import java.util.Scanner;

public class ChildProc {


    private static class StdinListenerThread extends Thread {

        public void run() {
            int c;
            try {
                c = System.in.read();
                while ( c != -1 ) {
                    System.out.print(c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("\nChild exits.");
            System.exit(0);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("I'm child process.");
        StdinListenerThread thread = new StdinListenerThread();
        thread.start();
        Thread.sleep(10000);
    }
}

After run this parent process by the following command:

    java process.parent_child.ParentProc

You will see

    I'm parent.
    I'm child process.
    Parent exits.
    xmpy-mbp:bin zhaoxm$
    Child exits

Child process exits immediately when parent process exits.

Solution 5

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.

Share:
57,765
Alex
Author by

Alex

I'm a rubyist working for Gemini SBS.

Updated on July 09, 2022

Comments

  • Alex
    Alex almost 2 years

    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:

  • jbwharris
    jbwharris over 15 years
    Killing a process normally doesn't allow it any sort of clean up. Which is exactly the problem.
  • Powerlord
    Powerlord over 15 years
    That depends on how you kill a process and under what OS. SIGTERM on Unixes allows the program to do cleanup, SIGKILL does not.
  • Aaron
    Aaron about 14 years
    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)
  • Esben Skov Pedersen
    Esben Skov Pedersen about 12 years
    This is a good solution. If reading is done in a thread. Remember to set thread deamon(true) or thread will stop program from shutting normally.
  • Rui Marques
    Rui Marques over 11 years
    If anyone is wondering, in Android systems pid seems to be 0 (process System pid) instead of 1, when parent dies.
  • Jan Spurny
    Jan Spurny about 10 years
    I've just implemented this "parent status checking" with one simple improvement - I just pass the original parent_pid to children and they just check to see if the parent pid stays the same. So it should work regardless of init/systemd/whatever pid numerical value.
  • NDestiny
    NDestiny over 8 years
    What If we launch some 3rd party process like Word, Excel. We can't code this child processes to pole its parent right ??
  • Admin
    Admin over 7 years
    nice solution, it was very helpful.
  • Carsen Daniel Yates
    Carsen Daniel Yates almost 6 years
    Runtime.getRuntime().addShutdownHook(new Thread(childProcess::destroy));
  • Jon N
    Jon N over 5 years
    This works nicely, and allows you to gracefully exit and avoid orphans of a child java process when the parent java process is terminated with a SIGKILL by Eclipse (for example).
  • ed22
    ed22 over 4 years
    What is the overhead of this solution?
  • mirabilos
    mirabilos almost 3 years
    You didn’t answer the “how” though; stackoverflow.com/a/272728/2171120 did (and IMHO ought to become the accepted answer, too).
  • Shane Gannon
    Shane Gannon over 2 years
    For me in Groovy this was Process proc = cmd.execute() addShutdownHook { proc.destroy() }