我想通过我的 java 应用程序搜索某个进程,以便我可以验证我的应用程序是否正在运行(Apache)。这是该类的简化。
package com.tecsys.sm.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.tecsys.sm.util.FinalValues;
public class ShellRuntimeCommands {
public static void showApacheProcess(){
try {
String command = "ps ax | grep \"/apps/apache/2.4.4/bin/httpd\" | grep -v \"grep\"";
System.out.println("La commande est: " + command);
final Process proc = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = br.readLine();
System.out.println("The Error line is "+line);
String status;
if(line!=null && line!=""){
status = FinalValues.STARTED_STATUS;
}else{
status = FinalValues.STOPPED_STATUS;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
ShellRuntimeCommands.showApacheProcess();
}
}
这是输出:
La commande est: ps ax | grep "/apps/apache/2.4.4/bin/httpd" | grep -v "grep"
The line is ERROR: Garbage option.
问题是我复制粘贴输出的字符串命令并在终端中执行,它工作正常,但它不能通过 java 运行时工作。