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

Fellow Monks,
I have a problem (actually I have severall :) but this is most important) in my script I have...
....
daemonize(); open(LOG, "tail -f $LOGFILE|") || die "cannot fork: $!"; while (<LOG>) { print "@_\n"; }

I use a fork(), redirect the STDS*, do a setsid() in my function daemonize.
My problem is, when my ppid is killed (dies) his child i.e. the "tail -f" binds to pid 1 (kernel). I want that when the parent is killed both should die.

Thanks in advance.
NA

Replies are listed 'Best First'.
Re: Hold on the child under parent
by dave_the_m (Monsignor) on Feb 18, 2005 at 14:55 UTC
    This is standard UNIX behaviour. To avoid it you either have to send a kill signal to the whole process group, or in the parent, trap the kill signal, and in the handler, send a kill signal to the child.

    Dave.

Re: Hold on the child under parent
by penantes (Acolyte) on Feb 18, 2005 at 15:18 UTC
    Dave thanks on the fast reply.
    I think I saw something like that in somebody elses code, if I understood allright my script (parent process) when receives a... lets say kill -9 he traps the signal and forwards it to every child he has?
    jbrugger, sorry about that.
    Regards,
    Nuno A
Re: Hold on the child under parent
by Crackers2 (Parson) on Feb 18, 2005 at 16:07 UTC

    Isn't the setsid() supposed to explicitely _prevent_ the behaviour you want? One of the properties of a daemon is that it won't die when the shell that started it exits.

    So I believe that if you just remove your setsid() call your child will die when the parent does.

Re: Hold on the child under parent
by jbrugger (Parson) on Feb 18, 2005 at 14:56 UTC
    hi penantes. Please use <code> tags when posting code here.
    open(LOG, "tail -f $LOGFILE|") || die "cannot fork: $!"; while (<LOG>) { ...blah blah blah
    *update* Hi, penantes, no need to say sorry, it had to be told to me the first time as well (i don't read the manuals as well ;-)

    Thing however really irritates me at PM, is that some people here downvote legitime postings WITHOUT a reason! Be a real monk and leave a node to zero like this. No need to up or downvote it, as well as no need to downvote ANY node without giving the reason imho.
    If i downvote, i'd like to give a reason, so the op may learn why.

      After a couple of days I follow the rule: Upvote only. No down-votes. I like this!