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?
source to share