in reply to Proc::Daemon and Log::Log4perl

Looking at the source code of Proc::Daemon, the relevant code has potential for failures but doesn't check the results:

sub Init { my $oldmode = shift || 0; my($pid, $sess_id, $i); ## Fork and exit parent if ($pid = Fork) { exit 0; } ## Detach ourselves from the terminal croak "Cannot detach from controlling terminal" unless $sess_id = POSIX::setsid(); ## Prevent possibility of acquiring a controling terminal if (!$oldmode) { $SIG{'HUP'} = 'IGNORE'; if ($pid = Fork) { exit 0; } } ## Change working directory chdir "/"; ## Clear file creation mask umask 0; ## Close open file descriptors foreach $i (0 .. OpenMax) { POSIX::close($i); } ## Reopen stderr, stdout, stdin to /dev/null open(STDIN, "+>/dev/null"); open(STDOUT, "+>&STDIN"); open(STDERR, "+>&STDIN"); $oldmode ? $sess_id : 0; }

There are a couple of possibilities for you to check:

  1. The code closes all files currently open. From how I understand Log::Log4Perl::FileAppender, it opens the file once and then tries to use the filehandle it got from that. So maybe you need to reopen your log files after having become a daemon.
  2. The code sets the umask to 0, so you might need to reset it in your daemon code to some sensible value if you're trying to create new files
  3. The code resets SIGHUP, so whatever you're doing with SIGHUP might conflict with that.
  4. The Proc::Daemon code doesn't check for the result of chdir("/") - maybe you are not allowed to set your directory to / - this should be checked but is likely not the cause of your problems.

For more debugging information, you should maybe reopen STDERR to a log file to which you warn (instead of using Log::Log4Perl) - I expect some print to closed file handle messages here.