簡體   English   中英

Java進程執行“ tail -f test.log | grep abc”無法獲得任何輸出

[英]Java process execute “tail -f test.log | grep abc” can't not get any output

我已經編寫了一個Java程序來執行linux命令。 但我發現它無法從命令“ tail -f test.log | grep abc”獲得任何輸出

以下是幾個示例:

"tail -n 100 test.log | grep abc" -- works
"tail -f test.log" -- works  
"tail -f test.log | grep abc" -- not works

我的Java代碼

String[] cmd = new String[] {"/bin/sh", "-c", "tail -f /Users/Alexis/Test/test.log | grep abc"};
InputStream inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
byte[] bb = new byte[8];
int i = 0;
while ((i = inputStream.read(bb)) != -1) {
    System.out.println("received : " + new String(bb, Charsets.UTF_8));
}

當我在test.log中回顯某些內容時,我無法獲得任何輸出。

最后我使用'tail -f test.log | unbuffer -p grep abc'並且它有效

另外,您可以使用'tail -f test.log | stdbuf -o0 grep abc'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM