dimanche 28 juin 2015

Find the process information with top command in java

I want to get the process information using the top command ,i have the below code but it is not working ,it just exists the program without any output .My objective is to get the processname,processid,memoryusage but that's the later part,I am stuck in getting process information using top command in java using grep.

public void getProcessInfo(String processName){

    Runtime rt = Runtime.getRuntime();
    try{
        String[] cmd = { "/bin/sh", "-c", "top | grep "+processName };

        Process proc = rt.exec(cmd);

        InputStream stdin = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(stdin);
        BufferedReader br = new BufferedReader(isr);

        String line = null;


        while ( (line = br.readLine()) != null){

            System.out.println(line);

        }



    }catch(Exception e){
        e.printStackTrace();
    }
}

}

Aucun commentaire:

Enregistrer un commentaire