http://www.perlmonks.org?node_id=568136

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

Hello all,

I wrote a little script to watch growing logfiles. It works nicely, just the output cannot be browsed if piped to less. Example usage:

terminal1 $ touch /tmp/my.log terminal2 $ perl script.pl -p . /tmp/my.log | less +F terminal1 $ echo "lala" >> /tmp/my.log terminal1 $ echo "lala" >> /tmp/my.log terminal1 $ ...
There is no visible output in terminal2, only if you interrupt less with ctrl-C.
#!/usr/bin/perl -w use strict; $| = 1; use Getopt::Std; my %opts; getopts( 'p:f:s:h', \%opts ); my $pattern = $opts{p} || "pdf"; my $sleep = $opts{'s'} || 5; my $logfile = shift || "/tmp/a.log"; my $fh = select( STDOUT ); $|=1; select( $fh ); print "watching file '$logfile' for pattern '$pattern'\n"; open(LOGFILE, "<" . $logfile) or die "not readable: '$logfile'$!\n"; seek(LOGFILE, 0, 2); while (1) { while (<LOGFILE>) { print if /$pattern/o; } sleep $sleep; seek(LOGFILE, 0, 1); }
Thanks for your time! Axel.