5

Normally if I create a process via something like:

Process proc = new ProcessBuilder("some_long_running_script.py").start();

and then my java program finishes, I can see that the script process continues to run (as expected)

if however I then add the code:

proc.waitFor();

and then kill my java program before the script finishes, I can see that the script dies as well (not as expected).

This would seem to say that "waitFor()" somehow merges the script process with my java process, is this by design ? I can't seem to see it discussed anywhere in the docs.

Could this be OS specific? I'm running on Mac Yosemite

Aharon Levine
  • 121
  • 1
  • 5
  • What version and implementation of Java are you using? According to the Javadocs for Process I actually think it should be the opposite that the sub process should die when Java dies. The Java PID owns the PID kicked off by the Process, all of its inputs and outputs belong to it and it should be killed when Java is killed by the OS. – maple_shaft Apr 22 '15 at 13:33
  • I'm using Java 7. Actually the Javadocs for Process says: The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object. " – Aharon Levine Apr 22 '15 at 13:48
  • 1
    Right... that says the subprocess continues in the native platform after the Process object is garbage collected. The Java native process still continues even after the Process object is gone. If the Java process is terminated by the native platform then any subprocesses natively attached to this Java native process will be killed as well. – maple_shaft Apr 22 '15 at 14:09
  • What is your Python script actually doing? Mind posting it to reproduce the behaviour? – Tunaki Jul 06 '16 at 19:46
  • 1
    Are you doing `.inheritIO()`? If not, you should. In general, I am observing that if you did `.inheritIO()`, then the spawned process _continues to run_. If you did not flush the buffers (by not calling `inheritIO`) then the subprocess becomes orphan (parented by the init process 1) and then dies. – Kedar Mhaswade Jul 06 '16 at 20:41

0 Answers0