1

How to check the CPU usage for a particular process(like tomcat) and for how much time it is consuming the same CPU amount programmatically ?

The problem is 8 tomcats has been configured, out of which 4 runs while the other 4 are stopped. Any time when any of the 4 tomcats goes on using same CPU amount for much longer time the app. hangs and so manually I have to kill it and start any other one. So I have to constantly keep an eye on the tomcats which is actually a dumb process.

So I need a solution to detect the span of time and the amount of CPU usage, and as soon as there is a problem the process(like tomcat) should be stopped/killed programmatically.

Sharmi
  • 79
  • 1
  • 1
  • 10
  • When you speak of CPU usage, do you mean User CPU? Do you mean percentage of User CPU? Do you want to know the aggregate percentage of CPU usage for the four Tomcat processes? Or Do you want to know the CPU usage for each of the Tomcat? There are a lot of questions here that need to be addressed before being able to answer your question. – Jordan Dea-Mattson Apr 11 '13 at 03:47
  • Yes, I mean to say percentage of CPU that the tomcats are consuming. Its like I want to track the percentage of CPU usage of each tomcat. – Sharmi Apr 11 '13 at 03:58

1 Answers1

1

If ur using windows...

Process proc = Runtime.getRuntime().exec ("tasklist.exe");
InputStream procOutput = proc.getInputStream ();
if (0 == proc.waitFor ()) {
// TODO scan the procOutput for your data
}

linux...

try {
    String line;
    Process p = Runtime.getRuntime().exec("ps -e");
    BufferedReader input =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //<-- Parse data here.
    }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56