得到指定进程的运行时间
http://blog.csdn.net/j00152685/archive/2010/11/25/6036169.aspx
【正文】实现要点
1. java调用shell命令(ps),并处理命令的返回结果
2. ps -ef | grep manager | grep -v grep | awk '{print $2}' 可以得到manager进程的PID
注意,这里会有一个问题,grep manager会将一些带有manager关键字的其他进程也找出来,导致错误,稍微正确一些的是:
ps -ef | grep 'manager$' | grep -v grep | awk '{print $2}'
用正则表达式限制一下要以manager结尾
3. ps -o etime -p [pid] 可以得到指定pid的进程的已经运行的时间
注意,在HP和HPAT机型上,需要设置环境变量UNIX95=1才能使用ps -o选项。其他(Linux、SUN、IBM)上不需要设置该环境变量。
- package org.snmp4j.agent.uvcagent;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ScpState
- {
- private static Log agentLog = Log.getInstance();
- /**
- * 调用GetScpPid判断manager的pid是否存在
- *
- */
- public static boolean isScpRun()
- {
- //调用GetScpState得到SCP的pid
- String scpPid = GetScpPid();
- if (scpPid.compareToIgnoreCase("") != 0)
- {
- //如果pid不为空,说明manager在运行
- //true表示SCP运行
- agentLog.getLog().debug("Manager is running");
- return true;
- }
- //false表示SCP不运行
- agentLog.getLog().debug("Manager is not running");
- return false;
- }
- /**
- * 通过ps -ef | grep manager | awk '{print $2}'
- * 得到manager的pid
- *
- * 如果pid不为空,说明manager是在运行的
- */
- public static String GetScpPid()
- {
- String scpPid = "";
- Runtime rt = Runtime.getRuntime();
- //shell命令
- String str[] = {"/bin/sh", "-c","ps -ef | grep manager | grep -v grep |awk '{print $2}'"};
- //String str[] = {""};
- Process pcs;
- try
- {
- agentLog.getLog()
- .debug("Run shell: ps -ef | grep manager | grep -v grep |awk '{print $2}'");
- pcs = rt.exec(str);
- BufferedReader br = new BufferedReader(new InputStreamReader(
- pcs.getInputStream()));
- //得到shell的执行结果
- scpPid = br.readLine();
- //当manager没有运行时,得到shell的返回值的,所以这里要特殊处理一下
- if(scpPid == null)
- {
- scpPid = "";
- }
- agentLog.getLog().debug("Get Pid of Manager: " + scpPid);
- try
- {
- pcs.waitFor();
- }
- catch (InterruptedException e)
- {
- agentLog.getLog()
- .error("Get InterruptedException: processes was interrupted");
- }
- br.close();
- pcs.exitValue();
- }
- catch (IOException e1)
- {
- agentLog.getLog().debug("Get IOException!");
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- return scpPid;
- }
- /**
- * 通过ps -o etime -p [mangaer pid]
- * 得到magager的运行时间
- *
- * 重点在etime
- *
- * HP和HPAT上需要先设置环境变量 UNIX95=1才能使用-o选项。
- */
- public static long GetScpRunTime(String Pid)
- {
- String str2[] = {"/bin/sh", "-c", "ps -o etime -p " + Pid};
- String scpRunTime = "";
- Runtime rt = Runtime.getRuntime();
- Process pcs2;
- try
- {
- System.out.println("ps -o etime -p " + Pid);
- agentLog.getLog().debug("Run shell: ps -o etime -p " + Pid);
- pcs2 = rt.exec(str2);
- BufferedReader br = new BufferedReader(new InputStreamReader(
- pcs2.getInputStream()));
- //当manager进程只有一个时,该命令应该返回两行,第二行是etime的时间
- String line = new String();
- while ((line = br.readLine()) != null)
- {
- scpRunTime = line.trim();
- }
- agentLog.getLog().debug("Get RunTime of Manager: " + scpRunTime);
- try
- {
- pcs2.waitFor();
- }
- catch (InterruptedException e)
- {
- agentLog.getLog()
- .error("Get InterruptedException: processes was interrupted");
- }
- br.close();
- pcs2.exitValue();
- }
- catch (IOException e1)
- {
- agentLog.getLog().debug("Get IOException!");
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- //转换etime的时间格式,为毫秒
- return ChangeTimeFormat(scpRunTime);
- }
- /**
- * 转换etime的时间格式[[dd-]hh:]mm:ss
- * 为毫秒
- */
- public static long ChangeTimeFormat(String etimeFormat)
- {
- String day = "";
- String hour = "";
- String minuter = "";
- String second = "";
- String tempEtimeFormat = etimeFormat;
- if (etimeFormat.indexOf("-") != -1)
- {
- day = etimeFormat.substring(0, etimeFormat.indexOf("-"));
- //tempEtimeFormat为[hh:]mm:ss
- tempEtimeFormat = etimeFormat.substring(etimeFormat.indexOf("-") + 1,
- etimeFormat.length());
- }
- //从etimeFormat中得到秒
- second = tempEtimeFormat.substring(tempEtimeFormat.lastIndexOf(":") + 1,
- tempEtimeFormat.length());
- tempEtimeFormat = tempEtimeFormat.substring(0,
- tempEtimeFormat.lastIndexOf(":"));
- minuter = tempEtimeFormat.substring(tempEtimeFormat.lastIndexOf(":") + 1,
- tempEtimeFormat.length());
- if (tempEtimeFormat.lastIndexOf(":") != -1)
- {
- tempEtimeFormat = tempEtimeFormat.substring(0,
- tempEtimeFormat.lastIndexOf(":"));
- hour = tempEtimeFormat;
- }
- Long dayLong = new Long(0);
- Long hourLong = new Long(0);
- Long mimuterLong = new Long(0);
- Long secondLong = new Long(0);
- if (day.compareTo("") != 0)
- {
- dayLong = Long.valueOf(day);
- }
- if (hour.compareTo("") != 0)
- {
- hourLong = Long.valueOf(hour);
- }
- mimuterLong = Long.valueOf(minuter);
- secondLong = Long.valueOf(second);
- //以毫秒为单位
- long scpRunTime = dayLong.longValue() * 24 * 60 * 60 * 1000
- + hourLong.longValue() * 60 * 60 * 1000
- + mimuterLong.longValue() * 60 * 1000 + secondLong.longValue()
- * 1000;
- agentLog.getLog().debug("Change time to millisecond: " + scpRunTime);
- return scpRunTime;
- }
- public static void main(String args[])
- {
- if(isScpRun())
- {
- System.out.println("Manger is running");
- String pid = GetScpPid();
- System.out.println("Pid is: " + pid);
- long time = GetScpRunTime(pid);
- System.out.println("Time is: " + time);
- }
- else
- {
- System.out.println("Manger is not running");
- }
- }
- }