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