יום רביעי, 27 בנובמבר 2013

Tail, grep, and stdio buffering

I was engaged in some troubleshooting once and needed to follow a log and specifically capture some string as soon as it appears in the log.
I have started my way, believing that using tail logfile | grep string will suffice, but after a few minutes of struggling to understand a certain delay between the actual server responses and the output on my screen I have realised there is some buffer at work here.
some google query afterwards I have came to acknowledge a known buffer that exists specifically in linux distributions (wasn’t able to replicate in solaris) which in order to override needs the flag --line-buffered added to grep.
so now that I run
 tail logfile | grep --line-buffered string
I expect to immediately (as possible) see the requested string shown on the stdio as soon as it is being written on the logfile.