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

2019-05-15 16:41发布

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?

3条回答
萌系小妹纸
2楼-- · 2019-05-15 17:13

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

查看更多
淡お忘
3楼-- · 2019-05-15 17:18

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.

查看更多
别忘想泡老子
4楼-- · 2019-05-15 17:18

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.

查看更多
登录 后发表回答