JAVA中执行linux命令


这里以“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;
        
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM