2013-10-15 23 views
1

我有一個循環,隨着它的增長,它將跟隨一個日誌文件,尋找一個模式並對其作出反應。問題是我在某個地方打了一個緩衝區,導致輸入循環延遲,我相信這是由於tail -f和管道日誌到grep最大限度地減少在bash中延遲循環處理的緩衝管道

不工作

while read l; do echo "l = |$l|"; done < <(tail -f $logfile | grep $pattern) 

我有安裝$logfile作爲fifo並具有cat realfile.log > $logfile 3或4倍(realfile.log是〜2K線),緩衝似乎以填充和線之前通過循環立即處理

如果我從重定向的stdin中刪除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) 

難道tailfsync()'ing-f寫操作?

+1

http://stackoverflow.com/questions/972370/how-do-you-pipe-in​​put-通過grep-to-another-utility – Iain

+0

lain,在我的搜索中沒有看到。 – mmlb

回答

3

答案就在下面:

如果我刪除重定向的標準輸入grep $pattern,該文件是 處理預期。

grep緩衝導致延遲的輸出。對grep使用--line-buffered選項可禁用緩衝。

man grep引用:

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

D'oh應該有grep的RTFM,而不是尾巴! – mmlb

1

嘗試增加行緩衝到的grep:

... grep --line-buffered $pattern ... 
+0

謝謝,我接受@ devnull的更完整一點。 – mmlb

相關問題