以编程方式检查任何进程(如tomcat)的CPU使用情况

时间:2013-04-11 03:38:51

标签: tomcat cpu-usage

如何检查特定进程(如tomcat)的CPU使用率以及以编程方式消耗相同CPU数量的时间?

问题是已经配置了8个tomcats,其中4个运行,而其他4个停止。任何时候当4个tomcats中的任何一个继续使用相同的CPU量相当长的时间应用程序。挂起,所以手动我必须杀死它并启动任何其他人。所以我必须时刻关注那些实际上是愚蠢过程的雄猫。

所以我需要一个解决方案来检测时间跨度和CPU使用量,一旦出现问题,就应该以编程方式停止/杀死进程(如tomcat)。

1 个答案:

答案 0 :(得分:1)

如果您使用的是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();
}