---------- Forwarded message ----------
From: Errin Larsen <[EMAIL PROTECTED]>
Date: Thu, 23 Sep 2004 16:30:21 -0500
Subject: Re: Daemon that starts other Daemons
To: Wiggins d Anconia <[EMAIL PROTECTED]>

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
<[EMAIL PROTECTED]> wrote:
>
>
> > On Thu, 23 Sep 2004 11:23:16 -0500, Errin Larsen
> <[EMAIL PROTECTED]> wrote:
> > > Hi perl-people,
> >
> > <<SNIP>>
> >
> > > So, my question is, how do I implement this code WITHOUT the parent
> > > process dieing?
> > >
> > > --Errin
> > >
> >

<<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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to