in reply to tee -f?

I've used something like this before:
use IO::File; use strict; my $tail = new IO::File; $tail->open("<./log.dat"); while(1){ my @lines=$tail->getlines(); if(0==scalar(@lines)){ # wait 1 second before trying again... # to keep from hogging CPU cycles sleep 1; }else{ my $line; foreach $line(@lines){ chomp $line; print " \"$line\"\n"; } } }
I've originally wrote this code w/ a select/can_read but it ended up hogging the CPU so I put in the 1 second sleep.

A few problems with this code:

  1. Up to a 1 second delay between data being flushed to the log and the program detecting it
  2. Can't detect EOF, keeps waiting for more data even after the writing program has closed the logfile. Of course, tail -f has this same problem.
The program writing to the logfile needs to be flushing its output or you may get irregular results (i.e. sometimes partial lines, etc....).

Replies are listed 'Best First'.
RE: Re: tee -f?
by Jonathan (Curate) on Jun 21, 2000 at 13:59 UTC
    Thanks, this looks like it will do the job nicely for what I need to do.