【发布时间】:2017-01-11 01:56:17
【问题描述】:
我在 java 中制作服务器监控应用程序。
我执行 shell 脚本(wget --no-cache -t 1 -T 1000 --spider ServerURL 2>&1 | grep 'HTTP')
我得到结果
但在 java exec shell 脚本中..
我没有得到结果...
源代码
String command = "wget --no-cache -t 1 -T 1000 --spider " + monitering_url +" 2>&1 | grep 'HTTP'";
shellCmd(command);
public static void shellCmd(String command) throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
//Thread.sleep(5000);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
moniting_url = serverURL
我想要结果打印...
帮帮我!!
【问题讨论】:
-
这需要shell解释(用于管道和重定向)。调用包含命令的脚本或将它们传递给解释器。
-
我解决了问题非常感谢!!