[plug] tail -f

Russell Steicke r.steicke at bom.gov.au
Fri Aug 12 11:14:04 WST 2005


On Fri, Aug 12, 2005 at 09:59:34AM +0800, Russell Steicke wrote:
> On Fri, Aug 12, 2005 at 11:14:12AM +0930, Onno Benschop wrote:
> > Thanks for the really quick response, but it's not working as I 
> > expected, this just sits there:
> > 
> > sudo /etc/init.d/fetchmail awaken && tail -n0 -f /var/log/syslog | grep 
> > fetch | awk '{print;} /sleeping/{exit;}'
> 
> Stdio buffering.  Assuming you're using GNU grep, try the
> --line-buffered flag to grep.
> 
> However there seems to be another wart.  When I tried this just now,
> awk didn't exit until it received the next line after the matching
> line.  I'm not sure why this is.

Worked it out.  grep doesn't exit until it gets a SIGPIPE, which
doesn't happen until it tries to write another line _after_ awk has
finished.  This probably won't be until the next fetchmail run.

Because of this pipeline problem, I think you'll have to do the whole
thing in one process, perhaps a perl script.

This works here, emulating tail -f and grep in a perl script:

< /var/log/syslog perl -e \
    '$|=1;
    seek(STDIN,0,2);
    while (1) {
      if (defined($line=readline(STDIN))) {
        if ($line =~ /qmail/) {
          print $line;
          if ($line =~ /end/) {
            exit;
          }
        }
      }
      else {
        sleep 1;
      }
    }'

(Looks for the end of a qmail delivery.  Adjust as required.)



-- 
Russell Steicke

-- Fortune says:
Center meeting at 4pm in 2C-543.



More information about the plug mailing list