我有一个循环,它将随着日志文件的增长而跟踪它,搜索模式并对其使用react。问题是我在某个地方遇到了一个缓冲区,延迟了循环获取输入的时间,我相信这是由于 tail -f
并将日志通过管道传输到 grep
造成的。 p>
不起作用
while read l; do echo "l = |$l|"; done < <(tail -f $logfile | grep $pattern)
我已将 $logfile
设置为 fifo
,并且必须 cat realfile.log > $logfile
3 或 4 次 (realfile.log大约 2K 行),在缓冲区似乎已填满并且通过循环立即处理这些行之前。
如果我从重定向的标准输入中删除 grep $pattern
,该文件将按预期进行处理。
有效
while read l; do echo "l = |$l|"; done < <(tail -f $logfile)
也有效
while read l; do echo "l = |$l|"; done < <(tail $logfile | grep $pattern)
是否 tail
没有对 -f
上的写入进行 fsync()'ing
?
最佳答案
答案如下:
If I remove the
grep $pattern
from the redirected stdin, the file is processed as expected.
grep
缓冲导致延迟的输出。使用 grep
的 --line-buffered
选项禁用缓冲。
引用man grep
:
--line-buffered
Use line buffering on output. This can cause a performance
penalty.
关于bash - 最小化 bash 中延迟循环处理的缓冲管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384464/