我正在尝试从 java 执行以下命令(在 unix 框中正确运行)-
kill -9 `ps -ef | grep -i myScript.sh | grep bash | awk '{print $2}'`
但是当通过Java执行时,我得到以下错误 -
/usr/bin/kill[8]: `ps: Arguments must be %job or process ids
Exit : 1
代码:
String command = "kill -9 `ps -ef | grep -i myScript.sh | grep bash | awk '{print $2}'`";
Process process=Runtime.getRuntime().exec(command);
InputStream stderr = process.getErrorStream();
InputStreamReader inputStream = new InputStreamReader(stderr);
BufferedReader reader = new BufferedReader(inputStream);
String error = null;
while((error = reader.readLine()) != null) {
System.out.println(error);
}
exitVal = process.waitFor();
System.out.println("Exit : "+ exitVal);
ps - 只是提醒一下,该命令在 shell 下运行得非常好。