I know how to "deamonize" a process (not to confused with Thread.setDaemon
). There are some answers here and here and I'm using my own perl wrapper, which works fine.
But what I'd like to get now, is the parent process waiting for the Java process until it says "OK", i.e., until it has really started successfully (not only process started, but everything up and running well).
I could indicate this by writing a file, binding to a socket or alike, but it's ugly. Out of the eight items on the deamonize list, I only need the following three in a slightly simplified form:
The first and last item can be done before process start, so only forking the process remains. Googling for "Java fork" is hopeless since the ForkJoinPool
exists. Before I get my hands dirty, I'd like to know if
I don't care about Windows as it's for a Linux server.
I don't know how well this translates to Java, but from a syscall perspective:
When both the parent and child are under your full control, you should call pipe
or socketpair
to create your own communication channel, and specify it to the child via environment variables or command-line arguments. Remember to immediately close one half in each process (either between fork and exec if you have control there, or via the CLOEXEC flag - this means the child executable never has to know about the parent's end at all).
I'm not sure why you seem hesitant to use sockets - perhaps you're under the impression that local sockets take up ports (they don't) - though if all the data travels in one direction I'd prefer pipes just for clarity.
If it is possible for the child to create its own children, you should set the CLOEXEC
flag on the inherited pipe as soon as possible.
You must send a positive message to indicate success; closing the pipe early must be considered an error (though you may also have explicit errors). Note that this means you don't have to track the exit value (useful if the "child" is actually a grandchild).
Alternatively, perhaps you should make something else do the work: your init system, X11, and DBUS? E.g., what were you thinking you should do if the child crashes?