参考链接:谭敏 18582562743《runtime无法执行grep_如何使管道使用Runtime.exec()?》https://blog.csdn.net/weixin_28736335/article/details/112051303
@Test
public void noticeRunTimeTest() throws IOException {
//注意`/usr/bin/bash`和`-c`左右两边不许有空格
String[] cmdArr = {"/usr/bin/bash", "-c", "ps -ef | grep test-app-1.0-SNAPSHOT.jar"};
Process process = Runtime.getRuntime().exec(cmdArr, null, null);
final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
System.out.println(line);
}
}
## 输出
user 5367 5357 0 09:06 pts/1 00:00:15 java -jar test-app-1.0-SNAPSHOT.jar
user 19297 19268 0 10:44 ? 00:00:00 /usr/bin/bash -c ps -ef | grep test-app-1.0-SNAPSHOT.jar
user 19299 19297 0 10:44 ? 00:00:00 grep test-app-1.0-SNAPSHOT.jar
注意/usr/bin/bash
和-c
左右两边不许有空格