Run as a daemon or service: Difference between revisions

From Rosetta Code
Content added Content deleted
(better description of a daemon thanks to ledrug)
m (→‎{{header|Pike}}: formatting)
Line 9: Line 9:


=={{header|Pike}}==
=={{header|Pike}}==
__FILE__ is a preprocessor definition that contains the current filename.
<code>__FILE__</code> is a preprocessor definition that contains the current filename.
if the first argument is "daemon" the program will be restarted with stdout redirected to "foo".
if the first argument is "daemon" the program will be restarted with stdout redirected to "foo".



Revision as of 09:40, 17 November 2011

Run as a daemon or service is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A daemon is a service that runs in the background independent of a users login session.

Demonstrate how a program disconnects from the terminal to run as a daemon in the background.

Write a small program that writes a message roughly once a second to its stdout which should be redirected to a file.

Note that in some language implementations it may not be possible to disconnect from the terminal, and instead the process needs to be started with stdout (and stdin) redirected to files before program start. If that is the case then a helper program to set up this redirection should be written in the language itself. A shell wrapper, as would be the usual solution on Unix systems, is not appropriate.

Pike

__FILE__ is a preprocessor definition that contains the current filename. if the first argument is "daemon" the program will be restarted with stdout redirected to "foo".

<lang Pike>int main(int argc, array argv) {

   if (sizeof(argv)>1 && argv[1] == "daemon")
   {
       Stdio.File newout = Stdio.File("foo", "wc");
       Process.spawn_pike(({ __FILE__ }), ([ "stdout":newout ]));
       return 1;
   }
   int i = 100;
   while(i--)
   {
       write(i+"\n");
       sleep(0.1);
   }

}</lang>