axelrose has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a little script to watch growing logfiles. It works nicely, just the output cannot be browsed if piped to less. Example usage:
There is no visible output in terminal2, only if you interrupt less with ctrl-C.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 $ ...
Thanks for your time! Axel.#!/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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: watch-script.pl | less +F buffering problem?
by liverpole (Monsignor) on Aug 18, 2006 at 14:01 UTC | |
by axelrose (Scribe) on Aug 18, 2006 at 14:13 UTC | |
by liverpole (Monsignor) on Aug 18, 2006 at 14:30 UTC | |
|
Re: watch-script.pl | less +F buffering problem?
by zentara (Cardinal) on Aug 18, 2006 at 13:44 UTC | |
by axelrose (Scribe) on Aug 18, 2006 at 13:54 UTC |