[英]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.