Daemon java process - is there such a thing?
Asked Answered
R

2

7

My java program creates a process in the following manner:

ProcessBuilder builder = new ProcessBuilder("phantomjs.exe crawl.js");
Process proc = builder.start();

In case the java program terminates abruptly (could always happen), the phantomjs process (which is not a java process) could stay alive and there would be no way to terminate it.

I want the phantomjs process to be terminated when the enclosing java process terminates (whether abruptly or not).

Is there a way to define the Process instance as a "daemon" object that terminates automatically when its super process (which is the java process executing the code above) terminates?

Rhombencephalon answered 10/3, 2016 at 15:4 Comment(9)
Why not use shutdown hooks?Pickett
Shutdown hook won't help when the process closes abruptlyRhombencephalon
@GentiSaliu I don't think the javascript tag is misleading. One might come up with a way to do what I need from within the javascript code that phantomjs executes.Rhombencephalon
Your question is ambiguous. "In case the java program terminates abruptly ... the phantomjs process ... could stay alive" - and "Is there a way to define the Process instance as a "daemon" object that terminates all of its sub-processes when terminating?" - are two different problems. Do you want the Process (phantomjs.exe) to terminate its children when it terminates, as you ask, or do you want the Process to be terminated when the containing Java process terminates?Pacian
Sorry if I wasn't clear enough. I want the phantomjs.exe process to terminate when its "father" java process terminates abruptly.Rhombencephalon
@Rhombencephalon in that case you need to edit your question, because it currently asks for something different.Pacian
@Pacian Done, 10x for letting me knowRhombencephalon
@Rhombencephalon it still says: "Is there a way to define the Process instance as a "daemon" object that terminates all of its sub-processes when terminating?" - and in your example, the Process instance is phantomjs.exe.Pacian
Alternatively you could setup a trigger in the Windows event logs for when an error event involving the JVM occurs, which terminates phantomjs.exe. See blogs.technet.com/b/wincat/archive/2011/08/25/…Pickett
P
1

The API documentation seems pretty definite, no qualifications or weasel-wording:

The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.

So the parent has to kill it, it won't go away when the parent terminates.

Pythian answered 10/3, 2016 at 16:22 Comment(2)
Yup, I read the documentation too. I wonder if there's any framework or 3rd party library that wraps the Process object and supplies the desired functionality.Rhombencephalon
@KidCrippler: right, I understand this provides limited value. i figured if this is not right and there is some kind of option someone will shoot it down.Pythian
Y
0

Not a great solution, but you could always check periodically the PID of the parent from the children.

Youth answered 20/4, 2021 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.