命令行过程在java中读取linux



我正在尝试执行这个:

Process p = Runtime.getRuntime().exec("ps -ef | grep bash");
BufferedReader r =  new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
System.out.println(line);
}

并希望看到一些行,但它返回Null.

如果我只执行"ps -ef"它返回所有进程正确。

通常我需要将关键字发送到方法,返回工作过程与否

这将起作用

String[] command =  {"/bin/sh", "-c", "ps -ef | grep bash"};
Process p = Runtime.getRuntime().exec(command);

最新更新