Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Apr 2023 00:25:36 -0700
From:      Pete <freebsd-questions-3@voidcaptain.com>
To:        Matthias Apitz <guru@unixarea.de>
Cc:        Christian Weisgerber <naddy@mips.inka.de>, questions@freebsd.org
Subject:   Re: Clogged pipe?
Message-ID:  <4b563b2a-2c26-8c84-f8db-51e4f17dcaf6@slagle.net>
In-Reply-To: <ZCu3TqNzhZBlDla7@pureos>
References:  <f8a7a545-9731-7481-4fb2-bfc77b8ad6f0@slagle.net> <ZCtavPuXQtYoV0TC@lorvorc.mips.inka.de> <d7565419-9dda-566c-5fc8-8815a43be3ca@slagle.net> <ZCu3TqNzhZBlDla7@pureos>

next in thread | previous in thread | raw e-mail | index | archive | help
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.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4b563b2a-2c26-8c84-f8db-51e4f17dcaf6>