How to get "instant" exit "tail -f" as input?

I want to keep track of a log file when a new log message matches my specific pattern (eg contain an "error"), then send me an email.

For this I wrote a python script monitor.py, the main part looks like this:

import sys

for line in sys.stdin:
    if "error" in line:
        print line 

      

Works well when I use tail my.log | python monitor.py

, then switch to tail -f my.log | python monitor.py

, then it doesn't work, at least not right away.

I've done some tests where new content in the log accumulates up to 8KB then my python script can get the output from the tail. So I highly suspect this is controlled by the size of the stdin / stdout buffer. How can I get the result right away?

Another question, when do I use tail -f my.log

and tail -f my.log | grep error

why can it show me the result right away?

+2


source to share


2 answers


Most Linux programs will use line buffering if stdout otherwise connects to TTY and full buffering. You can use stdbuf

to force buffering of lines.



stdbuf -oL tail -f my.log | python monitor.py

      

+4


source


There's a patch there to add unbuffered output to the tail , starting in 2008, which seems to have been rejected , and my own (BSD) manpage doesn't list it. Perhaps you could download coreutils , apply the patch, compile the tail yourself and still work?



0


source







All Articles