Saturday, August 7, 2010

tail, grep, and logger (aka the output is being buffered!)

QUESTION :

When I do the following in bash shell, it works well.
[root@lwlx root]# tail -f -n 1 /u/email.log | logger

But, when I integrate the grep command, it stops working and not producing any output.
[root@lwlx root]# tail -f -n 1 /u/email.log | grep Blocked | logger

ANSWER :

The output of (tail -f) is being buffered. The above won't work. Please see the workaround as follow.

To release the buffer whenever there are new lines in the log if you suspect that the output is being buffered :

tail -f -n 1 /u/email.log | perl -ne 'BEGIN{$|=1}print if /blocked/' | logger

What if I want two or more filters such as "Blocked" or "Denied" or "Blacklisted":

tail -f -n 1 /u/email.log | perl -ne 'BEGIN{$|=1}print if /Blocked|Denied|Blacklisted/'

Cheers.

1 comment:

  1. Thanks Buddy for the simple and elegant solution. So what I have to logger for a particular level and facility, how would be I doing it?

    ReplyDelete