[Chicago-talk] writing a log with Cron, Daemons, Perl

Jardine, Richard RJardine at allstate.com
Fri Sep 5 11:03:00 CDT 2003

I have never used Net::Server::Daemonize. I call a subroutine to turn the process into a daemon and redirect the output to a log.  This has been running without problems in a production program for a couple of years.

sub daemonize {
        my $log = '/var/tmp/log/allutilServer.log';
        chdir '/'                       or die "Can't chdir to /: $!";
        open STDIN, '/dev/null'         or die  "Can't read /dev/null: $!";
        open STDOUT, ">>$log"           or die "Can't write to $log: $!";
        open STDERR, ">>$log"           or die "Can't write to $log: $!";
        defined(my $pid = fork)         or die "Can't Fork: $!";
        exit if $pid;
        setsid                          or die "Can't start a new session: $!";
        umask 0;

-----Original Message-----
From: Jay Strauss [mailto:me at heyjay.com]
Sent: Friday, September 05, 2003 9:51 AM
To: chicago-pm
Subject: [Chicago-talk] writing a log with Cron, Daemons, Perl

I'm using Net::Server::Daemonize qw(daemonize) to turn my program into a

sometimes I start this program from the command line, some times via cron.
I'd like to have a log file that contains everything that would normally go
to STDERR plus all the messages my program spits out.

I'm running into a couple of problems:

1) how do I get stuff into my log file without closing the file handle?  I
tried $|++;  For example:

use Net::Server::Daemonize qw(daemonize);
daemonize ("jstrauss","jstrauss","/home/jstrauss/test.pid");
open (LOG,">/home/jstrauss/bin/test.log");
print LOG "Help\n";
while (1) {

my message never gets printed to the file.

2) how would I redirect all of STDERR so that my messages appear in the
proper order?  for example:
open (LOG,">/home/jstrauss/bin/test.log");
open (STDERR, ">>&LOG");
print LOG "yep\n";
die "you jerk";

:~/bin> cat test.log
you jerk at /home/jstrauss/bin/jay line 16.

*** notice the messages above are in the reverse order that they occured in
the program.

Lastly, is there some error logging module I should be using?

Chicago-talk mailing list
Chicago-talk at mail.pm.org

More information about the Chicago-talk mailing list