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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |