这里以“df-h”和“free-mh”为例
/** * 检测服务器 * @throws Exception */ public static Map<String,Object> checkServer( ) throws Exception{ Map<String,Object> map=new HashMap<String,Object>(); String allline= ""; try { List<String> list1=new ArrayList<>(); List<String> list2=new ArrayList<>(); Process process = null; BufferedReader reader = null; //| awk '{print $2 }' String[] cmd = new String[]{"/bin/sh","-c", " df -h"}; process = Runtime.getRuntime().exec(cmd); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = ""; boolean foundFlag = false ;//找到 指定设备在运行的标记 默认为未找到 while ((line = reader.readLine()) != null) { list1.add(line); } //拼接 reader.close(); process.destroy(); //| awk '{print $2 }' cmd = new String[] { "/bin/sh", "-c", " free -mh" }; process = Runtime.getRuntime().exec(cmd); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String allline2=""; line = ""; foundFlag = false;// 找到 指定设备在运行的标记 默认为未找到 while ((line = reader.readLine()) != null) { list2.add(line); } reader.close(); process.destroy(); map.put("str1", list1); map.put("str2", list2); } catch (Exception e) { //e.printStackTrace(); } return map; }