Re: Clogged pipe?

From: Pete <freebsd-questions-3_at_voidcaptain.com>
Date: Tue, 04 Apr 2023 07:25:36 UTC
Matthias Apitz wrote on 4/3/23 22:36:
> El día lunes, abril 03, 2023 a las 08:38:15 -0700, Pete escribió:
> 
>> So, I guess the short answer is that cat -n does block buffering to the
>> pipe, but cat with no options does not.
>>
>>    tail -f testfile | cat     | sed -u 's/^/X/'
>>    tail -f testfile | cat -u  | sed -u 's/^/X/'
>>    tail -f testfile | cat -nu | sed -u 's/^/X/'
>>
>> all provide immediate output, but
>>
>>    tail -f testfile | cat -n  | sed -u 's/^/X/'
>>
>> does not and waits.

> i.e. the sed(1) and the cat(1) are just reading STDIN waiting for
> more input to appear. The problem is with the "tail -f"
> The -f flag let the tail(1) after it has read all the lines of
> "testfile" just waiting for the file cwto grow which
> could happen if other processes would write to the file.
> 
> Nothing magic, wrong usage of -f here.


Well, I was just showing a simplified example to illustrate the problem; 
what I was really trying to script is this:

/usr/bin/tail -f -n+1 /var/log/exim/main-$(/bin/date -u '+%Y%m%d').log |
/bin/cat -n |
/usr/bin/sed -Eu 's/^[[:blank:]]*([[:digit:]]*)[[:blank:]]+/\1 /'

which prefixes line numbers as it watches today's exim log file scroll 
along. So, in this case the -f flag is needed on the tail command.

What actually fixes the buffering problem on FreeBSD (it already works 
fine on Linux) is to add the -u flag to the cat command.