[Chicago-talk] writing a log with Cron, Daemons, Perl
Jay Strauss
me at heyjay.com
Mon Sep 8 07:21:26 CDT 2003
Thanks Richard,
I sorta like the Net::Server::Daemonize because:
1) it's already written :)
2) it writes the pid of the child to a file, so that you can kill it later
I don't like that it points STDOUT and STDERR to /dev/null, seems like I
should be able to tell it where to point those (maybe it does, I'll have to
re-read the doc)
Thanks
Jay
----- Original Message -----
From: "Jardine, Richard" <RJardine at allstate.com>
To: "Chicago.pm chatter" <chicago-talk at mail.pm.org>
Sent: Friday, September 05, 2003 11:03 AM
Subject: RE: [Chicago-talk] writing a log with Cron, Daemons, Perl
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
daemon.
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:
#!/usr/bin/perl
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.
yep
*** 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?
Jay
_______________________________________________
Chicago-talk mailing list
Chicago-talk at mail.pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk
_______________________________________________
Chicago-talk mailing list
Chicago-talk at mail.pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk
More information about the Chicago-talk
mailing list