[plug] tail -f

Onno Benschop onno at itmaze.com.au
Sun Aug 14 08:16:44 WST 2005


Russell Steicke wrote:

>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.)
>
>
>
>  
>
Whoot, that inspired me to write one in PHP, not quite bash, but you get 
that :)

---START---
#! /usr/bin/php
<?

define('cr',"\n") ;

if ($argc != 3) {
    printf('Usage: %s {showPattern} {stopPattern}'.cr.cr,$argv[0]) ;
    echo '{showPattern} is used as a filter to only show lines 
containing this text.'.cr ;
    echo '{stopPattern} is used to determine when to halt execution.'.cr ;
    exit ;
}

while ($line = fread(STDIN,8192)) {
    if (strpos($line,$argv[1])!==FALSE) {
        echo $line ;
    }
    if (strpos($line,$argv[2])!==FALSE) {
        exit ;
    }
    sleep (1);
}

?>
---ENDS---

I'm still keen to see if I can find a bash solution, but your 
contribution sparked some ideas, thanks.

-- 
Onno Benschop

Connected via Optus B3 at S34°45'36.5" - E139°00'08.7" (Mount Pleasant, SA)
--
()/)/)()        ..ASCII for Onno..
|>>?            ..EBCDIC for Onno..
--- -. -. ---   ..Morse for Onno..

Proudly supported by Skipper Trucks, Highway1, Concept AV, Sony Central, Dalcon
ITmaze   -   ABN: 56 178 057 063   -  ph: 04 1219 8888   -   onno at itmaze.com.au





More information about the plug mailing list