1

I need to get the CPU usage fro a particular process from java code in LINUX.

I tried

Process p = Runtime.getRuntime().exec("top");

or

Process p = Runtime.getRuntime().exec("top | grep java");

But It's not returning any thing. Actually I want to get the CPU usages of several JBOSS nodes. If I can "top" or something inside the java code then I can process the out put an find the details that I want.

But if I try

Process p = Runtime.getRuntime().exec("ls -l");

Its returning the out put correctly.

Is there I can do like this or are there any other options to do this?

thanks.

Chamith Malinda
  • 4,399
  • 5
  • 24
  • 28
  • 1
    http://stackoverflow.com/questions/47177/how-to-monitor-the-computers-cpu-memory-and-disk-usage-in-java – mre Oct 26 '12 at 16:40

2 Answers2

2

If you know the process IDs, and you are the user that started them (or you've got sufficient privilege) you could always get the details by reading the pseudo-file /proc/PID/stat. There's a good description of how to do this in answer to this question: How to calculate the CPU usage of a process by PID in Linux from C?.

Community
  • 1
  • 1
Neil Winton
  • 286
  • 1
  • 5
  • Yes this is a good way. But the problem is I have to know the PID already. for that I had to do an another system call. But many thanks for the link – Chamith Malinda Oct 26 '12 at 19:19
0

This should work I believe...

Runtime.getRuntime().exec("ps -ao pcpu,args | grep java");

or top -b | grep java if you insist on using top

nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • No this is not working. Command line it's working but not in the java code. – Chamith Malinda Oct 26 '12 at 17:09
  • How exactly is it not working? Do you get any output? Have you tried both of my suggestions? – nullpotent Oct 26 '12 at 17:09
  • Process p = Runtime.getRuntime().exec("top -b | grep java"); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = in.readLine()) != null) { System.out.println(s); } Process p = Runtime.getRuntime().exec("ps -ao pcpu,args | grep java"); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = in.readLine()) != null) { System.out.println(s); } But no any out put – Chamith Malinda Oct 26 '12 at 17:16
  • can you check the error stream to see if you have any errors returned? I am not sure you can run all commands with exec as some require the shell to be running first. to get the error stream do : BufferedReader err= new BufferedReader(new InputStreamReader(process.getErrorStream())); and then print from it. to run a shell and then the command in it, try Process p = Runtime.getRuntime().exec(new String[] {"bash","-c","top | grep java" } ); you can also replace the last argument in the array with the command given in this answer. –  Oct 26 '12 at 17:46
  • for : BufferedReader err= new BufferedReader(new InputStreamReader(process.getErrorStream())); >> TERM environment variable not set. – Chamith Malinda Oct 26 '12 at 18:11
  • did you try running with array of parameters? also I suggest you should change the title of the question as this is more related to java exec that getting a process cpu usage. –  Oct 26 '12 at 18:31
  • How can I run with array of parameters ? – Chamith Malinda Oct 27 '12 at 06:02