0

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.

John Smith
  • 777
  • 2
  • 14
  • 37
  • 2
    possible duplicate of [How to get list of running processes with Java](http://stackoverflow.com/questions/20472639/how-to-get-list-of-running-processes-with-java) – Joe Jul 12 '15 at 14:14

1 Answers1

0

To get list of all running processes you should use command

ps

It will list them all and will complete. The command has many command line options, for example show processes not only for current user, but for all users on the machine.

ya_pulser
  • 2,620
  • 2
  • 16
  • 21