views:

43

answers:

2

Normally, we started applications from the terminals(or console), and the terminal which starts the applications will be the standard io of the applications.

  1. If we close the terminals, the applications started by the terminal will normally be closed as well. But for those daemon processes are not closed. My questions is how terminal decides which applications to close, which not? And if I wanna write a daemon application, what shall I do to prevent being closed?

  2. For those applications not closed, what happens if they print out something to the standout. Since the terminal is closed, where will the output go?

Thanks.

A: 

For your first question, I am assuming unix-like. If you want a daemon in a unix-like systems, you use something like: java& . Note the ampersand at the end of the command - that tells the shell to run it as a daemon.

For your second question, if I remember correctly, unless you pipe standard out somewhere else (like a file) it is simply swallowed when the process is a daemon process.

Hope that helps.


edit: struck out the ampersand background portion as per Dirk's comment.


Additional edit: Take a look at the Wikipedia article on Job Control. It has interesting information, especially about using disown along with a background process to turn it into a daemon.

aperkins
Oh, and to answer your question about how the terminal "decides" what to close - it doesn't. Daemon processes are not linked to a particular user session, while non-daemon processes are. When the terminal/shell is killed, the processes linked with it are terminated, while those that are not are kept alive. Again, assuming unix-like behavior and OS, although Windows does similar behavior, it simply calls it different things.
aperkins
Dirk
@Dirk I was not aware of that, and I was pretty sure that my experiences with that were different. However, it has been a few years since I have attempted to do that, so I defer to your wisdom.
aperkins
Guoqin
@Gouqin however, if you use the disown process, it should turn it into a daemon. That was the piece I had forgotten - sorry about that.
aperkins
@aperkins, thank you very much. But this is control outside the codes, do you know how to manipulate in codes themselves, just like some applications: sshd, httpd, samd, synergys, etc...
Guoqin
guys, I found a good explanation in Wiki: http://en.wikipedia.org/wiki/Daemon_%28computing%29
Guoqin
and their explanation of "Dissociating from the controlling tty" is what disown SHOULD be doing, as per my understanding.
aperkins
+1  A: 

Try the Apache JSVC package. It provides a proper executable, which knows how to arrange things in order for a java process to run as daemon under unix. It will also take care of the stdout/err channels, redirecting them into files or whatever you need. For windows, the procrun tool provides the capabilities to run a java VM as standard windows service.

Dirk