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

I have a process that I have daemonized using the recipe found in the cookbook (17.15). My problem is that once I open a filehandle for writing a log file (which does not generate a warning) the daemon does not redirect to it, despite my best efforts to print to the FILEHANDLE. If anyone has encountered this before, help would be appreciated.

If you make something idiot-proof, eventually someone will make a better idiot.
I am that better idiot.

Replies are listed 'Best First'.
Re: Problem writing to file from a daemon
by peschkaj (Pilgrim) on Nov 11, 2002 at 19:13 UTC

    I solved the problem quite simply. First, I read the fscking manual a third time and I noticed that while I was autoflushing the buffer on STDOUT, I wasn't flushing the buffer on the filehandle.

    I fixed this by using autoflush on the filehandle:

    open (LOGFILE, ">tempLog.$time" ) || die "Cannot open tempLog for writ +ing: $!\n"; autoflush LOGFILE 1; print LOGFILE "Socket server started on port $port at ", localtime, "\ +n";
    If you make something idiot-proof, eventually someone will make a better idiot.
    I am that better idiot.
Re: Problem writing to file from a daemon
by pg (Canon) on Nov 11, 2002 at 19:16 UTC
    Not clear about your actual situation, but the following code does prove that child processes being forked would be aware of the files opened by parents. But when to close is a little bit tricky, the sync stuff.
    use strict; my $file; open($file, ">abc"); my $ret = fork; if ($ret) { print $file "log from parent\n"; sleep(5); close($file); } else { print $file "log from child\n"; }

      I solved the problem of when to close by trapping for close signals from the terminal (kill -1, kill -9) and including close handles in the event handler.

      If you make something idiot-proof, eventually someone will make a better idiot.
      I am that better idiot.
Re: Problem writing to file from a daemon
by dmitri (Priest) on Nov 11, 2002 at 19:10 UTC
    Could you please list your code? Blind debugging is hard.