I'm trying to get a list of all running processes from Ubuntu using Java.
I tried to run "top" and parse the response.
My problem is that "top" command starts a process that can be killed by the user.
I tried this:
public void execute()
{
try{
ProcessBuilder build = new ProcessBuilder("top");
Process proc = build.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while((line = reader.readLine()) != null)
{System.out.println("wait");
System.out.println(line);
}
proc.waitFor();
reader.close();
}
catch(IOException e)
{
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I was unable to get a response because the command is still executing.
How to stop the process and then to get its response ? (and this again since I want to update the informations)
EDIT: Unique parts: "top" command displays detailed informations about the running processes(like mem consumption, cpu), this data is constantly updated thus it is not possible to simply run it and take the response.