大家好试图将我的文件托管在开发以及跳转服务器上,不得不使用ps -ef命令。 grep tomcat。有人知道命令的实际作用吗?以及它实际上如何工作?
答案 0 :(得分:0)
命令ps -ef | grep tomcat
列出了在进程参数中包含tomcat字词的进程。要使用Java语言执行此类命令,必须使用Runtime.exec()
或ProcessBuilder
。示例代码附在下面。
import java.util.*;
import java.io.*;
public class Test {
public static void main(String args[]) {
Process p = null;
String command = "ps -ef | grep tomcat";
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;
while ((line = br[0].readLine()) != null){
System.out.println(line);
}
try {
br[0].close();
} catch (Exception a) {}
try {
br[1].close();
} catch (Exception a) {}
}
catch (Exception grrr) {
}
finally {
try {
closeStreams(p);
p.destroy();
} catch (Exception r) {}
}
}
static void closeStreams(Process p) throws IOException {
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
}
}