I have done this sort of thing myself with no difficulty. I suspect the reason you are having a problem, but I did not is because you are using Proc::Daemon and the like, but I wrote the code snippet to daemonize the process myself.

I suspect that Proc::Daemon is closing ALL filehandles, including the one that log4perl just opened to the logfile, but that is just a guess.

Here is an extract from one of my scripts that does what you need:

# log4perl configuration. my $log4perlConf = q( # Normal messages to a log file. (That gets rotated once a week) log4perl.appender.logMessages=Log::Dispatch::FileRotate log4perl.appender.logMessages.filename=/var/log/logMessages.log log4perl.appender.logMessages.mode=append log4perl.appender.logMessages.max=5 # Number of logfiles kept be +fore deletion. log4perl.appender.logMessages.DatePattern=yyyy-ww log4perl.appender.logMessages.TZ=GMT log4perl.appender.logMessages.layout=Log::Log4perl::Layout::Patter +nLayout log4perl.appender.logMessages.layout.ConversionPattern=%d %p> %F{1 +}:%L %M - %m%n log4perl.logger = DEBUG, logMessages ); my $stop_now = 0; # Set to true by the signal ha +ndler. $SIG{'INT'} = sub { $stop_now = 1 }; sub main { # Setup log4perl Log::Log4perl::init(\$log4perlConf); my $logger = Log::Log4perl->get_logger(); my $opts = getArgs(); # Wrappers a call to Getopt::Long my $user_agent = LWP::UserAgent->new(); # Connect once & make sure it all works. fetchParseLog( $opts, $user_agent ); # Writes logging messages +via log4perl daemonize() if $opts->{'daemonize'}; # Main sleep, fetch & parse, sleep cycle. MAINLOOP: while( 0 == $stop_now ) { fetchParseLog( $opts, $user_agent ); # Writes logging messa +ges via log4perl sleep $sleep_time; } return 0; } sub daemonize { my $logger = Log::Log4perl->get_logger(); my $pid = fork(); defined $pid or $logger->logdie( "Can't fork: $OS_ERROR" ); if( $pid ) # Parent { exit 0; } elsif( 0 == $pid ) { # Child # Reopen filehandles to null, to prevent all output. open STDIN, '<', '/dev/null' or $logger->logdie( "Can't read + from /dev/null: $OS_ERROR" ); open STDOUT, '>>', '/dev/null' or $logger->logdie( "Can't writ +e to /dev/null: $OS_ERROR" ); open STDERR, '>>', '/dev/null' or $logger->logdie( "Can't writ +e to /dev/null: $OS_ERROR" ); # Become a session leader (no longer associated with the paren +t shell & TTY). setsid or $logger->logdie( "Can't star +t a new session: $OS_ERROR" ); # All done return; } }

In reply to Re: Daemon w/ logging before and after background by chrestomanci
in thread Daemon w/ logging before and after background by batkins61

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.