onegative has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Monks,

I am trying to determine what might be a way to remember a location in a Log file if my FIFO process dies or is killed. Currently the code I have does maintain its location within a growing log, but if the process dies or is killed I lose my location. I was wondering if I write my tell() information to a file on each iteration, and then have the code read this information only upon a new startup of the process would it work? But what I am not sure of is how would I fall back into my loop using the seek() information. Or if anyone has a better way of maintaining a growing log. I have seen the File::Tail module, but am having trouble getting it to work. Anyway, here's my FIFO code. Thanks in advance.

# Preset Values my $LogFile="/usr/local/apache2/eventmgr/ncomount/page.log"; my $PipeName="/usr/local/apache2/pipes/.nco_p_data"; my $naptime=5; my $curpos; open(LOGFILE, "<$LogFile") || die "Couldn't open file $Status - $!\n" +; for(;;){ for($curpos=tell(LOGFILE);LOGFILE;$curpos=tell(LOGFILE)){ while($line=<LOGFILE>){ # print $line; if($line =~ /(.*)\|~\|(.*)\|~\|(.*)/){ $PagerID=$1; $Message=$2.$3; } unless( -p $PipeName) { unlink $PipeName; system("mkfifo -m 644 $PipeName") && die "Can' +t mkfifo $PipeName: $!"; } open(FIFO,">$PipeName") || die "Couldn't open +file $PipeName - $!\n"; print FIFO "NEW|TODAY|NOW|\"\"|$PagerID|$Messa +ge\n"; close(FIFO); } sleep(1); } sleep $naptime; seek(LOGFILE, $curpos, 0); # seek to where we had been }

Replies are listed 'Best First'.
Re: Remembering Log Location after process dies.
by matija (Priest) on Mar 04, 2004 at 20:59 UTC
    I'm the author of File::Tail. What trouble are you having in getting it to work?
      Howdy Mr Grabnar, I am using the following to try and read the file but I get no output. I installed the module and get no warnings about it, but I can't seem to get any output. And the log file does exist. Here's the code I am using:
      #!/usr/bin/perl -w use File::Tail; my $LogFile="/usr/local/apache2/eventmgr/ncomount/page.log"; my $file=File::Tail->new("$LogFile"); while (defined($line=$file->read)) { print "$line"; }
        Your code looks OK. So let me list a few reasons why you might not get any output:
        • By deafult, File::Tail starts reading at the file's end. So if nothing has been written to the file from the time when you opened it to the time when you kill the script, you won't get any data.

          Solution: Write some data to the file, or start File::Tail with          $file=File::Tail->new(name=>$name, tail=>10);.

          That will tell File::Tail to first feed you the last ten lines in the file, and then wait for new data.

        • You just wrote some data, but File::Tail has not returned any yet.

          File::Tail strives to be as efficient as possible, and place as little unneeded load on the system as possible. To accomplish this, File::Tail remembers how long it has been since it last found new data in the file. And it sleeps that long before checking the file again.

          Solution:Wait a few minutes (if it has been long from the time when you opened the file to first new data), or start File::Tail by explicitly specifying the maximum idle time:

          $file=File::Tail->new(name=>$name, maxinterval=>10);
          With that, File::Tail will never wait more than 10 seconds before checking the file.