【发布时间】:2022-01-23 10:21:31
【问题描述】:
如果有任何代码可以找到任何其他进程的 PID,我搜索并检查了 SO。 有一种解决方案可以创建一个带有命令“ps -eaf | grep myprocess”的 shell 脚本并从 java 执行该脚本。
但我想使用 java ProcessBuilder 或 Runtime 方法执行。下面是我尝试过的代码,它没有给我 null 作为输出。
import java.io.*;
public class TestShellCommand {
public static void main(String args[]) {
Process p = null;
String command = "ps -ef | grep myProcess";
try {
// p = new ProcessBuilder(command).start();
p = Runtime.getRuntime().exec(command);
BufferedReader br[] = new BufferedReader[2];
br[1] = new BufferedReader(new InputStreamReader(p.getErrorStream()));
br[0] = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
if(br[0].readLine() == null){
System.out.println("The input stream is null.");
}
while ((line = br[0].readLine()) != null) {
System.out.println(line);
}
try {
br[0].close();
} catch (Exception a) {
a.printStackTrace();
}
try {
br[1].close();
} catch (Exception a) {
a.printStackTrace();
}
} catch (Exception grrr) {
grrr.printStackTrace();
} finally {
try {
closeStreams(p);
p.destroy();
} catch (Exception r) {
r.printStackTrace();
}
}
}
static void closeStreams(Process p) throws IOException {
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
}
}
命令的输出是:
java TestShellCommand
The input stream is null.
{sdc@ip-172-31-32-49}[26] echo $?
0
如果我的代码中有任何错误,请告诉我,因为当我从 shell 手动搜索时,我确实得到了预期的输出,如下所示:
ps -ef | grep myProcess
root 7433 1 0 10:33 ? 00:00:00 myProcess hello
sdc 19894 14130 0 11:24 pts/7 00:00:00 grep myProcess
[更新的代码 - 没有 grep 命令]
import java.io.*;
public class TestShellCommand {
public static void main(String args[]) {
Process p = null;
String [] command = {"ps", "-eaf"};
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br[] = new BufferedReader[2];
br[1] = new BufferedReader(new InputStreamReader(p.getErrorStream()));
br[0] = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
if(br[0].readLine() == null){
System.out.println("The input stream is null.");
}
while ((line = br[0].readLine()) != null) {
System.out.println(line);
}
// Then code to find by process name by using string methods ...
try {
br[0].close();
} catch (Exception a) {
a.printStackTrace();
}
try {
br[1].close();
} catch (Exception a) {
a.printStackTrace();
}
} catch (Exception grrr) {
grrr.printStackTrace();
} finally {
try {
closeStreams(p);
p.destroy();
} catch (Exception r) {
r.printStackTrace();
}
}
}
static void closeStreams(Process p) throws IOException {
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
}
}
当我传递命令时,我已经添加了有效的代码:
-
new String[]{"/bin/sh","-c", "ps -eaf | grep "+ "myProcess" +" | grep -v grep"}- 空响应。 -
new String[] {"ps", "-eaf", "grep -m 1 myProcess", "awk -F ' ' '{print $2}' "}- 空响应。
提前感谢任何线索。
【问题讨论】:
-
"不给我 null 作为输出" 你能解释一下你的意思吗?你有一个空的 catch 块,所以可能你得到一个异常并忽略它。
-
也可以考虑在没有shell脚本的情况下运行ps命令,并在代码中做grep部分。
-
@tgdavies 请参阅我已更新代码和输出以确认输出为空。
-
如果有任何问题,请查看 stderr。
-
@Subhajit 简单地更改使用的 cmd 并不能解决您的问题。您需要根据两个都说以特定方式使用“ProcessBuilder”的答案来修复代码 - 您没有阅读上面的 stderr 流。
标签: java shell process runtime processbuilder