I don't think a running process can switch itself from foreground to background, detaching from its input and output. A suitably sophisticated shell can, of course, use job management to background running tasks (in most UNIX shells, ctrl-Z
, fg
, bg
etc.).
In Java the best I can think of is to write two programs:
- your long-running server runs in the background, and starts by opening a
ServerSocket
, it listens for one connection, on which it expects to receive the authentication info. Only when that has happened does it initialise fully and go into its main listening loop.
- another program runs in the foreground. It waits for the socket to become available, connects, prompts at the terminal for a password, writes that to the socket, closes and terminates
You'll need to do some simple scripting to launch both programs at the same time. There's no reason why both programs have to be Java -- on UNIX I'd be tempted to use netcat to squirt the password into the server.
For security, ensure that the socket uses localhost networking. On UNIX it could instead be a UNIX domain socket.
Of course there are lots of communication methods your two processes could use -- files, shared memory, pipes, TCP sockets, ... -- it's a matter of picking one that's secure enough for the information you're passing between them.