Talk:Run as a daemon or service: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 22: Line 22:
Also, why do you want a daemon process to both detach from stdout ''and'' print to stdout? Wouldn't it be saner to have it directly write to a file to begin with? --[[User:Ledrug|Ledrug]] 08:23, 17 November 2011 (UTC)
Also, why do you want a daemon process to both detach from stdout ''and'' print to stdout? Wouldn't it be saner to have it directly write to a file to begin with? --[[User:Ledrug|Ledrug]] 08:23, 17 November 2011 (UTC)
: I want it to detach from the stdout that is connected to the users terminal, and write to a file as if it were stdout. Redirecting stdout is the sanest way to achieve that because the internals of the program and the libraries used can continue to write to what looks like stdout. If I open a file in the daemon itself and use that, I have to go out of my way to make sure that everywhere I have output the file is used. I have to make that file globally accessible or pass it around as a variable. There may be languages/situations where this is the better option, but I would not say that it is always better or saner.--[[User:EMBee|eMBee]] 08:56, 17 November 2011 (UTC)
: I want it to detach from the stdout that is connected to the users terminal, and write to a file as if it were stdout. Redirecting stdout is the sanest way to achieve that because the internals of the program and the libraries used can continue to write to what looks like stdout. If I open a file in the daemon itself and use that, I have to go out of my way to make sure that everywhere I have output the file is used. I have to make that file globally accessible or pass it around as a variable. There may be languages/situations where this is the better option, but I would not say that it is always better or saner.--[[User:EMBee|eMBee]] 08:56, 17 November 2011 (UTC)
:: This strikes me as bad modularity. If you want the program detached from the shell's stdio context you can and should do that in the shell. You should not replicate that shell-specific code in every program. It's an unnecessary complication for programs running from inittab, for example. For example, from bourne shell:
:: This strikes me as bad modularity. If you want the program detached from the shell's stdio context you can and should do that in the shell. You should not replicate that shell-specific code in every program.
::: i do not understand this point. every daemon would need its own shellscript because they all have different files their output needs to go to. there is nothing that shellscripts would avoid from being replicated.

::: also, depending on the language, the code to detach the daemon may be no more complex than the shellscript.--[[User:EMBee|eMBee]] 15:15, 17 November 2011 (UTC)
:: It's an unnecessary complication for programs running from inittab, for example. For example, from bourne shell:
::<lang bash>(myprog </dev/null >/dev/null 2>&1 &)</lang>
::<lang bash>(myprog </dev/null >/dev/null 2>&1 &)</lang>

::This detaches <code>myprog</code> from the shell (its parent process will be init, and it obviously will not be using the shell's terminal). And, of course, if you want to reconfigure it to log to a file (or to another process) from stdout, you could do that instead.
::This detaches <code>myprog</code> from the shell (its parent process will be init, and it obviously will not be using the shell's terminal). And, of course, if you want to reconfigure it to log to a file (or to another process) from stdout, you could do that instead.

::But if you are really serious about running myprog as a daemon, you should probably run it from inittab (or xinittab or whatever ...), probably wrapped in <code>su</code> and so it runs as a relevant user. --[[User:Rdm|Rdm]] 14:42, 17 November 2011 (UTC)
::But if you are really serious about running myprog as a daemon, you should probably run it from inittab (or xinittab or whatever ...), probably wrapped in <code>su</code> and so it runs as a relevant user. --[[User:Rdm|Rdm]] 14:42, 17 November 2011 (UTC)
::: an unnecesary complication is having to write a shell script in order to run a daemon. X and getty are run from inittab without a shell and we can probably find other examples, of course we can find counter examples too, but i don't see a reason prefer using a shell wrapper.
::: also there are only few developers writing the daemon, but many more sysadmins running it. so any complication that is taken away from the sysadmin and moved to the developers is a win in the long run.
::: but the main point really is to show of the capabilities of languages here. using a shell script would defeat that purpose and would not fit on rosettacode. it also would not be portable.--[[User:EMBee|eMBee]] 15:15, 17 November 2011 (UTC)

Revision as of 15:15, 17 November 2011

Bad definition?

It's probably better to describe a daemon as a process that runs in the background and independent of a user's login session, if anything.

True, I'll try to integrate that into the description.--eMBee 08:56, 17 November 2011 (UTC)

"Not connected to a terminal" isn't quite it, a daemon could print to a tty all day if it wants to.

Yes, but only if it opens the tty manually. It should otherwise be able to run without a tty being present.--eMBee 08:56, 17 November 2011 (UTC)
Not really. <lang c>#include <stdio.h>
  1. include <stdlib.h>

int main(void) {

       FILE *fp = fopen("/tmp/outlog", "w");
       while (1) {
               printf("yes\n");
               fprintf(fp, "out\n");
               fflush(fp);
               sleep(1);
       }

}</lang>

Run it as ./a.out & disown in bash, and it will keep printing to the terminal. It qualifies as a daemon in most senses because it will keep going (verify by the outlog file) even if you log out and kill the terminal. It can run without a tty, doesn't mean "without a tty" is a necessary condition. --Ledrug 09:56, 17 November 2011 (UTC)
fair enough. however i would not consider that a clean way to write a daemon. the goal here is to write the daemon in such a way that the administrator does not have to jump through hoops to make sure that the process doesn't die if the terminal goes away and to capture all output from the daemon.--eMBee 13:14, 17 November 2011 (UTC)

Also, why do you want a daemon process to both detach from stdout and print to stdout? Wouldn't it be saner to have it directly write to a file to begin with? --Ledrug 08:23, 17 November 2011 (UTC)

I want it to detach from the stdout that is connected to the users terminal, and write to a file as if it were stdout. Redirecting stdout is the sanest way to achieve that because the internals of the program and the libraries used can continue to write to what looks like stdout. If I open a file in the daemon itself and use that, I have to go out of my way to make sure that everywhere I have output the file is used. I have to make that file globally accessible or pass it around as a variable. There may be languages/situations where this is the better option, but I would not say that it is always better or saner.--eMBee 08:56, 17 November 2011 (UTC)
This strikes me as bad modularity. If you want the program detached from the shell's stdio context you can and should do that in the shell. You should not replicate that shell-specific code in every program.
i do not understand this point. every daemon would need its own shellscript because they all have different files their output needs to go to. there is nothing that shellscripts would avoid from being replicated.
also, depending on the language, the code to detach the daemon may be no more complex than the shellscript.--eMBee 15:15, 17 November 2011 (UTC)
It's an unnecessary complication for programs running from inittab, for example. For example, from bourne shell:
<lang bash>(myprog </dev/null >/dev/null 2>&1 &)</lang>
This detaches myprog from the shell (its parent process will be init, and it obviously will not be using the shell's terminal). And, of course, if you want to reconfigure it to log to a file (or to another process) from stdout, you could do that instead.
But if you are really serious about running myprog as a daemon, you should probably run it from inittab (or xinittab or whatever ...), probably wrapped in su and so it runs as a relevant user. --Rdm 14:42, 17 November 2011 (UTC)
an unnecesary complication is having to write a shell script in order to run a daemon. X and getty are run from inittab without a shell and we can probably find other examples, of course we can find counter examples too, but i don't see a reason prefer using a shell wrapper.
also there are only few developers writing the daemon, but many more sysadmins running it. so any complication that is taken away from the sysadmin and moved to the developers is a win in the long run.
but the main point really is to show of the capabilities of languages here. using a shell script would defeat that purpose and would not fit on rosettacode. it also would not be portable.--eMBee 15:15, 17 November 2011 (UTC)