[英]linux shell script exec(wget) in java
我在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);
}
}
monitering_url = serverURL
我要打印結果...
幫我!!
您的命令是一個Shell腳本,必須傳遞給Shell進行解釋:
Process process = runtime.exec(new String[] {"/bin/sh", "-c", command});
請注意使用數組將參數傳遞給Shell進程。 沒有它,命令字符串將在傳遞給解釋器之前被標記化,這不是您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.