Is it possible to double-fork a process in Java?

2019-05-15 17:15发布

问题:

I need to double-fork vmware so it doesn't inherit the terminal ID (TTY/pts).

This is what I have so far, but I can't get access to the runtime.exec process to fork another process (which removes the terminal ID).

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("vmware");

Is there a way to "daemonize" in Java?

回答1:

You will need to write a small C or C++ program to do the extra fork, setsid, all that stuff.



回答2:

To make your life easier with this, you essentially need to disconnect the application from the shell by closing the input / output streams that are implicitly connected to the current terminal.

To make this simpler fro yourself, have a look at the akuma project. This would be a cleaner solution in plain Java without any need for C wrappers.

Here is a blog post discussing it.



回答3:

You don't actually need to "double fork" in order to do this, only a single fork is necessary. Then the child should close stdin, out, error and start a new session.