[英]Java process execute “tail -f test.log | grep abc” can't not get any output
I've written a java program to execute linux command. 我已经编写了一个Java程序来执行linux命令。 But I find it can not get any output from the command "tail -f test.log | grep abc"
但我发现它无法从命令“ tail -f test.log | grep abc”获得任何输出
Below are several examples: 以下是几个示例:
"tail -n 100 test.log | grep abc" -- works
"tail -f test.log" -- works
"tail -f test.log | grep abc" -- not works
My Java code 我的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));
}
When I echo something into test.log, I can't get any output. 当我在test.log中回显某些内容时,我无法获得任何输出。
finally I use 'tail -f test.log | 最后我使用'tail -f test.log | unbuffer -p grep abc' and it works
unbuffer -p grep abc'并且它有效
Also, you can use 'tail -f test.log | 另外,您可以使用'tail -f test.log | stdbuf -o0 grep abc'
stdbuf -o0 grep abc'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.