01
String cmd = "";
02
Runtime rt = Runtime.getRuntime();
03
Process p = null;
04
try {
05
p = rt.exec(cmd);
06
} catch (Exception e) {
07
08
} finally {
09
if (p != null) {
10
try {
11
p.waitFor();
12
} catch (InterruptedException e) {
13
e.printStackTrace();
14
}
15
p.destroy();
16
}
17
}
以前也用過Process執行外部命令,比如備份mysql之內的。一直沒用關閉過Process。這次寫監控在linux環境中獲取內存和cpu等信息定期發送jms到監控服務器。由於沒有關閉process,在運行一段時間之後文件描述符被用完導致無法登陸系統瞭。然後才知道原來這個也是要關閉的。。代碼如上。
作者:絕望的八皮