Professional Web Applications Themes

Daemon that starts other Daemons - PERL Beginners

Hi perl-people, I'm not sure if this is beginners stuff, but I'll post here 'cause it's the only list I'm subscribed to at the moment. I'm writing a script that will daemonize itself, and then watch some processes. If one of those processes die, it will start it again. So, I've been reading the perlipc docs and I found this handy code on proper daemonization: use POSIX 'setsid'; sub daemonize { #it's polite for daemons to chdir to root so that they #don't prevent a filesystem from being unmounted chdir '/' or die "Can't chdir to /: $!"; #it's also ...

  1. #1

    Default Daemon that starts other Daemons

    Hi perl-people,

    I'm not sure if this is beginners stuff, but I'll post here 'cause
    it's the only list I'm subscribed to at the moment.

    I'm writing a script that will daemonize itself, and then watch some
    processes. If one of those processes die, it will start it again.
    So, I've been reading the perlipc docs and I found this handy code on
    proper daemonization:

    use POSIX 'setsid';

    sub daemonize {
    #it's polite for daemons to chdir to root so that they
    #don't prevent a filesystem from being unmounted
    chdir '/' or die "Can't chdir to /: $!";

    #it's also polite for daemons to redirect all output to
    #/dev/null so users don't get random output
    open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
    open STDOUT, '>/dev/null' or die "Can't write to /dev/null:$!";

    #the parent get's the new child's pid back, the child gets '0' back
    defined( my $pid = fork ) or die "Can't fork: $!";

    #here's where I start having problem. This code assumes that
    #the parent will be exiting, thus leaving the child able
    #to run setsid
    exit if $pid;
    setsid or die "Can't start a new session: $!";

    open STDERR, '>&STDOUT' or die "Can't dup STDOUT: $!";
    }

    perlipc goes on to explain:
    "The fork() has to come before the setsid() to ensure that you
    aren't a process leader (the setsid() will fail if you are).

    So, my question is, how do I implement this code WITHOUT the parent
    process dieing?

    --Errin
    Errin Guest

  2. #2

    Default Re: Daemon that starts other Daemons

    On Thu, 23 Sep 2004 11:23:16 -0500, Errin Ln <com> wrote: 

    <<SNIP>>
     

    I found that (at least on the Solaris OS that I'm working on) that the
    setsid function will setup a new session UNLESS:
    " The calling process is already a process group leader,
    or the process group ID of a process other than the
    calling process matches the process ID of the calling
    process. "

    So, I think that's saying that as long as the process (the child) does
    not have any children of it's own, then I'll be ok with the above
    code! Is that what the blurb above is saying? (Why are UNIX docs
    always so darn hard to read!!?)
    Errin Guest

  3. #3

    Default Re: Daemon that starts other Daemons

    > Hi perl-people, 

    A stretch, but there have been more complex topics discussed. This is
    pretty much a catch all and some of the experts will probably appreciate
    not answering the same how do I delete an element of an array question :-).
     

    Great docs... however...
     

    Actually you want the parent process to "die"... or at least exit, then
    your child process is the session leader and will do everything else you
    need, aka fork more children in your case. Essentially you want to make
    it so that the tty/shell (whatever) no longer thinks it has absentee
    children so that it can exit cleanly, by daemonizing your process
    essentially becomes a child of the kernel, which will always be running
    (until shutdown that is).

    However, the best advice NOT given by perldoc perlipc is to use the
    Proc::Daemon::Init module, it is incredibly easy and takes care of all
    of the code you mention above by itself, in a neat package that you
    don't have to worry about the internals of. It is available through
    CPAN as usual, and despite its incredibly "low release number" (if you
    are into that hole thing) I have never had problems with it.

    Two additional comments, that you are likely to stumble across:

    1. How do I get back to my process after it has daemonized, how do I
    stop it, etc?

    This is where you will likely want to get into writing a "pid" file to
    the disk so that your process can be looked up and signaled, and/or to
    prevent two copies of your daemon running, or at least trying to work on
    teh same resources.

    2. How do I do logging, etc.?

    Check out the very, very excellent Log::Log4perl suite, it is incredible
    and can accomplish just about all of your logging needs.

    Of course I have already mentioned POE.... but I will plug it again.

    http://danconia.org
    Wiggins Guest

  4. #4

    Default Re: Daemon that starts other Daemons

    > On Thu, 23 Sep 2004 11:23:16 -0500, Errin Ln
    <com> wrote: 
    >
    > <<SNIP>>

    >
    > I found that (at least on the Solaris OS that I'm working on) that the
    > setsid function will setup a new session UNLESS:
    > " The calling process is already a process group leader,
    > or the process group ID of a process other than the
    > calling process matches the process ID of the calling
    > process. "
    >
    > So, I think that's saying that as long as the process (the child) does
    > not have any children of it's own, then I'll be ok with the above
    > code! Is that what the blurb above is saying? (Why are UNIX docs
    > always so darn hard to read!!?)
    >[/ref]

    That is the way I read it, though see the suggestion in my other post...

    http://danconia.org
    Wiggins Guest

  5. #5

    Default Daemon that starts other Daemons

    ---------- Forwarded message ----------
    From: Errin Ln <com>
    Date: Thu, 23 Sep 2004 16:30:21 -0500
    Subject: Re: Daemon that starts other Daemons
    To: Wiggins d Anconia <org>

    Hi again,

    Ok ... so with some research and playi^H^H^H^H^Htesting I've found the
    answer to what's really been bothering me. If I fork(), I get the PID
    of the resulting child. However, if THAT child runs and external
    command, how do I get the (now grand)child's PID. The answer I was
    looking for was a deeper understanding of exec().

    by fork()ing a child and having that child run exec(), the exec()ed
    command will have the SAME process ID as the original child. In
    actuality, the exec()ed command is NOT a grandchild, but has taken
    over the original child's process and all it's environment (STDIN,
    STDOUT, %ENV, etc.). That's what I needed to know! Now, if I just
    collect and keep the child's PID, when I run the exec() I'll have the
    PID of whatever command was exec()ed.

    Also, with some experimenting, the setsid() doesn't NEED the original
    parent to die to work, it just needs the child to be forked before it
    runs setsid(). In other words, if you try to run setsid() BEFORE you
    fork the child (in an attempt to give both the child and the parent
    the same session and group ID, perhaps) it will fail. If you instead
    have the child run setsid() after it is fork()ed, it will run fine,
    whether the parent is dead or not! YAY!!


    On Thu, 23 Sep 2004 13:30:08 -0600, Wiggins d Anconia
    <org> wrote: 
    > <com> wrote: 
    > >
    > > <<SNIP>>
    > > 
    > >[/ref][/ref]

    <<SNIP>>

    Thanks for the help, Wiggins. I agree this would all be easier if I
    just went a grabbed a module, but for (probably strange) reasons I'd
    rather not go into, I want to do this from scratch. Also, this has
    been extremely helpful in teaching me what's going on with the
    backticks, system(), fork() and exec() functions. This process has
    really helped me along with my Perl education.

    Also, I realized that the ORIGINAL parent needs to die (So as to
    disassociate the daemons from the calling terminal/process), but I was
    looking for a daemon that would run, start other servers, and that
    hang around monitoring them. I think we're talking about the same
    thing here, just I didn't explain it will originally.

    Now, I just need to implement some code to dump a file with PIDs into
    /var/run! Thanks for the suggestions/help and I'll get working on it
    now!

    --Errin
    Errin Guest

  6. #6

    Default Re: Daemon that starts other Daemons

    > Hi again, 

    Yep. Playing is the best way to go.
     

    Correct.
     

    Yep again, making the child a session leader doesn't mean that the
    parent must go away, more that if it wants to it can. Which is a subtle
    but important distinction.
     
    > > <com> wrote: [/ref]
    >
    > <<SNIP>>
    >
    > Thanks for the help, Wiggins. I agree this would all be easier if I
    > just went a grabbed a module, but for (probably strange) reasons I'd
    > rather not go into, I want to do this from scratch. Also, this has
    > been extremely helpful in teaching me what's going on with the
    > backticks, system(), fork() and exec() functions. This process has
    > really helped me along with my Perl education.
    >[/ref]

    Definitely. You are essentially taking the same path through the madness
    that I did 2 years ago or so, first attempting to understand the reasons
    and lowlevels, reading lots of docs and making lots of mistakes. Once I
    figured it out I was very glad because of the amount I learned, but then
    opted for the module mostly to prevent code maintenance, hassles, etc.
    We already had roughly 40 other dependencies, and considering it was
    probably the easiest to install I wasn't concerned about Just Another
    Module (hmph, JAM, that works pretty well in the glue ogy ;-)).
     

    Agreed and yep.
     

    http://danconia.org
    Wiggins Guest

  7. #7

    Default Re: Daemon that starts other Daemons

    On Thu, 23 Sep 2004, Errin Ln wrote:
     

    In other words, you want something that works like Apache [1.x].

    * To launch Apache, you run apachectl, a shell script.

    * apachectl launches a parent httpd process, which in turns spawns
    a pool of listener httpd child processes.

    * apachectl goes away then, and the parent httpd supervises the
    operation of the server from then on.

    Granted, the interesting bits are all done in C or something, so that
    may not help you here, but it's the model you're looking for.



    --
    Chris Devers
    Chris Guest

  8. #8

    Default Re: Daemon that starts other Daemons

    > On Thu, 23 Sep 2004, Errin Ln wrote: 
    >
    > In other words, you want something that works like Apache [1.x].
    >
    > * To launch Apache, you run apachectl, a shell script.
    >
    > * apachectl launches a parent httpd process, which in turns spawns
    > a pool of listener httpd child processes.
    >
    > * apachectl goes away then, and the parent httpd supervises the
    > operation of the server from then on.
    >
    > Granted, the interesting bits are all done in C or something, so that
    > may not help you here, but it's the model you're looking for.
    >
    >
    >
    > --
    > Chris Devers
    >[/ref]

    If you really want to get into it, Network Programming with Perl has
    excellent coverage of various common types of daemons and how to write
    them in Perl. Don't know if you have the resources or time but it is
    worth a look if the interest is there. My very unprofessional and
    non-expert review of it is here:

    http://danconia.org/cgi-bin/request?handler=Content;content=Site_Bookshelf_Boo k;id=26

    http://danconia.org

    Wiggins Guest

  9. #9

    Default Re: Daemon that starts other Daemons

    Errin Ln wrote: 

    If you *really* want to understand the nuts and bolts of all this, Stevens'
    _Advanced Programming in the UNIX Environment_ is a must.

    http://www.amazon.com/exec/obidos/ASIN/0201563177

    Bob Guest

  10. #10

    Default Re: Daemon that starts other Daemons

    Bob Showalter wrote: 
    >
    > If you *really* want to understand the nuts and bolts of all this, Stevens'
    > _Advanced Programming in the UNIX Environment_ is a must.
    >
    > http://www.amazon.com/exec/obidos/ASIN/0201563177[/ref]

    Since the OP mentioned that he is using Solaris he may want to get this book
    instead:

    http://www.rite-group.com/rich/ssp/index.html


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Audio starts on click/stops/and finaly starts correctwith video
    By N steht f?r? in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: December 19th, 02:30 PM
  2. daemons & emails
    By SINNER in forum Ubuntu
    Replies: 4
    Last Post: March 7th, 06:12 PM
  3. Can php be used for daemons like this?
    By James in forum PHP Development
    Replies: 8
    Last Post: February 12th, 02:03 AM
  4. Daemons on Mac OS X
    By Jorge in forum Mac Programming
    Replies: 7
    Last Post: September 23rd, 04:44 PM
  5. Replies: 2
    Last Post: September 15th, 08:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139