在CLI程序之间管道输出时缓冲问题 [英] Buffering problem when piping output between CLI programs

查看:93
本文介绍了在CLI程序之间管道输出时缓冲问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这很好用:



<$> p $ p> tail -fn0 /var/log/apache2/error.log | egrep -vPHP通知|文件不存在

但有一些字面意思是\\\
在我想用一个实际的新行代替的输出中,所以我管入perl:

$ $ $ $ $ $ $ $尾巴-fn0 / var / log / apache2 / error.log | egrep -vPHP通知|文件不存在| perl -ne's \\\\\\\\\\\\打印$ _'

这似乎有一些缓存问题(第一页打不出任何内容,第二页面点击和两个负载调试信息出来),它也似乎有点tempramental。

所以我试过sed:

  tail -fn0 /var/log/apache2/error.log | egrep -vPHP通知|文件不存在| sed's / \\\\\\\\\\\\\'\\\\\\\\\\'\\\\\\\'同样的问题。

解决方案

正确,当你使用大多数程序来输出文件或管道时,你可以在某些情况下控制它:GNU grep 系列接受 - 行缓冲的选项,专门用于在这样的管道中。另外,在Perl中,您可以使用 $ | = 1; 具有相同的效果。 ( sed 没有任何我知道的选项。)



这是将会缓冲的流水线的开始或中间,而不是结束(这是与您的终端交谈,因此它将被行缓冲),所以你想要使用 egrep --line-buffered


I'm trying to tail apache error logs through a few filters.

This works perfectly:

tail -fn0 /var/log/apache2/error.log | egrep -v "PHP Notice|File does not exist"

but there are some literal "\n" in the output which I want to replace with an actual new line so I pipe into perl:

tail -fn0 /var/log/apache2/error.log | egrep -v "PHP Notice|File does not exist" | perl -ne 's/\\n/\n/g; print"$_"'

This seems to have some caching issue (first page hit produces nothing, second page hit and two loads of debugging info comes out), It also seems a bit tempramental.

So I tried sed:

tail -fn0 /var/log/apache2/error.log | egrep -v "PHP Notice|File does not exist" | sed 's/\\n/\n/g'

which seems to suffer the same problem.

解决方案

Correct, when you use most programs to file or pipe they buffer output. You can control this in some cases: the GNU grep family accepts the --line-buffered option, specifically for use in pipelines like this. Also, in Perl you can use $| = 1; for the same effect. (sed doesn't have any such option that I'm aware of.)

It's the stuff at the beginning or middle of the pipeline that will be buffering, not the end (which is talking to your terminal so it will be line buffered) so you want to use egrep --line-buffered.

这篇关于在CLI程序之间管道输出时缓冲问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆