Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is more a theory question than coding question. At least in the sense that no code exists, and shouldn't need to in order to answer the question.

If one has a script, that they want to run as a deamon at startup, in linux and windows 2000, how would this be done?

in linux it would start via the /etc/init.d/ same as other system deamons. On windows it would need to start via the system services.

Replies are listed 'Best First'.
Re: perl daemon
by dws (Chancellor) on Apr 05, 2003 at 00:40 UTC
    Details for writing daemons differ between Unices (including Linux) and WinNT/Win2000/WinXP.

    Lincoln Stein covers the basics for Unix in Chapter 10, Forking Servers and the inetd Daemon, and Chapter 14, Bulletproofing Servers, in Network Programming with Perl.

    There's a bit more to being a well-behaved daemon than is obvious at first glance. On page 312, Stein notes that a well-behaved daemon should:

    1. "Autobackground" itself (fork/parent-exit), then close file handles and lose any assocition with a controlling terminal.
    2. Change the current working directory to a known directory.
    3. Change the file creation mask to a known state.
    4. Set the PATH to a known state.
    5. Record its process ID in a known place
    6. Optionally use syslog to write diagnostics
    7. Optionally handle HUP signals (e.g. to reload configuration files)
    8. Options use chroot()
    He then spends the better part of a chapter fleshing that out and giving examples.

      I've been quite impressed with Net::Daemon so far. It does most of what you listed there right out of the box and appears to be quite portable (though admittedly I have not tried to run it under windows).

      -Matt

Re: perl daemon
by chromatic (Archbishop) on Apr 05, 2003 at 00:13 UTC

    If you're asking if you're on the right track, I think so. In theory, one would start it via /etc/init.d in Linux and the system services in Windows, just as you say.

    Do you have a more specific question? It's difficult to understand exactly what you're asking as you seem to have answered yourself quite nicely.

      Lets assume the following piece of code:
      #/usr/bin/perl -w use strict; my $count = 0; while (1) { open(FILE,"filename.txt"); print FILE "$count - \n"; close(FILE); sleep(100); count++; }
      If i wanted that piece of code to run at startup, does anything special need to be done with it, to run it from /etc/init.d

      If the init script is as follows (cut down for example purposes):
      case "$1" in start) daemon /usr/local/bin/myscript.pl ;; stop) #stop stuff here ;; esac
        perldoc perlipc has a section on creating daemon processes (for UNIX).
        For Windows, you may not need to do anything special other than adding the script as a service but I'm no Windows expert.

        Hope this helps.

        --perlplexer
Re: perl daemon
by Jenda (Abbot) on Apr 06, 2003 at 21:27 UTC

    I'm afraid you will have to test the value of $^O and load some OS specific modules or define some subroutines. I can't comment on the Unix side, but you might end up with something like:

    BEGIN { if ($^O eq 'MSWin32') { eval <<'*END*'; use Win32::Daemon::Simple Service => 'SERVICENAME', Name => 'SERVICE NAME', Version => 'x.x', Info => { display => 'SERVICEDISPLAYNAME', description => 'SERVICEDESCRIPTION', }, Params => {}; *END* die $@ if $@; } else { eval <<'*END*'; # do whatever you need to make it a daemon under Unix sub DoEvents { # check the value of the variable you set in $SIG{...} handler +s # and stop if asked to } *END* die $@ if $@; } } while (1) { # do something sleep(60); DoEvents(); } __END__

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Re: perl daemon
by Anonymous Monk on Apr 06, 2003 at 01:17 UTC