user3203425
user3203425

Reputation: 3059

How to issue "top" command from a java app?

I'm trying to use the "top" command from a standalone java app. I basically want to run this:

top -p myProcessId -n 1 -b

my java psuedocode:

Process process = Runtime.getRuntime().exec("top -p myProcessId -n 1 -b");

// read both:
process.getInputStream();
process.getErrorStream()

and the output I get is:

top: failed tty get

I'm not sure what that output means - any ideas how to use "top" correctly here? I'm on ubuntu fwiw.

Thanks

----- Update -------------

OK I was able to do this instead with:

ps -Ao pid,rsz,cmd

and then I read through each output line for the process I'm interested in, I can read the res mem value from there.

Upvotes: 0

Views: 1349

Answers (1)

top doesn't use the ordinary standard output stream; it manipulates the terminal device directly. This means that you can't just read in its output like you can from most command-line programs. There are a few other programs that might do what you need (ps is a likely candidate), or you might be able to read the relevant information from /proc.

Upvotes: 1

Related Questions