gpt4 book ai didi

bash - 最小化延迟 bash 中循环处理的缓冲管道

转载 作者:行者123 更新时间:2023-11-29 09:44:09 25 4
gpt4 key购买 nike

我有一个循环,随着日志文件的增长,它会跟踪日志文件,搜索模式并对其使用react。问题是我在某个地方遇到了一个缓冲区,它延迟了循环获取输入,我相信这是由于 tail -f 并将日志传输到 grep

不起作用

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 不是 fsync()'ing -f 上的写入?

最佳答案

答案如下:

If I remove the grep $pattern from the redirected stdin, the file is processed as expected.

grep 缓冲导致延迟 的输出。使用 --line-buffered 选项为 grep 禁用缓冲。

引用自 man grep:

   --line-buffered
Use line buffering on output. This can cause a performance
penalty.

关于bash - 最小化延迟 bash 中循环处理的缓冲管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com