【转】 得到指定进程的运行时间

  得到指定进程的运行时间

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)上不需要设置该环境变量。

 

  1. package org.snmp4j.agent.uvcagent;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7.   
  8. public class ScpState  
  9. {  
  10.       
  11.     private static Log agentLog = Log.getInstance();  
  12.       
  13.     /** 
  14.      * 调用GetScpPid判断manager的pid是否存在 
  15.      *  
  16.      */  
  17.     public static boolean isScpRun()  
  18.     {  
  19.         //调用GetScpState得到SCP的pid  
  20.         String scpPid = GetScpPid();  
  21.           
  22.         if (scpPid.compareToIgnoreCase("") != 0)  
  23.         {  
  24.             //如果pid不为空,说明manager在运行  
  25.             //true表示SCP运行  
  26.               
  27.             agentLog.getLog().debug("Manager is running");  
  28.             return true;  
  29.         }  
  30.           
  31.         //false表示SCP不运行  
  32.         agentLog.getLog().debug("Manager is not running");  
  33.           
  34.         return false;  
  35.     }  
  36.       
  37.     /** 
  38.      * 通过ps -ef | grep manager | awk '{print $2}' 
  39.      * 得到manager的pid 
  40.      *  
  41.      * 如果pid不为空,说明manager是在运行的 
  42.      */  
  43.     public static String GetScpPid()  
  44.     {  
  45.           
  46.         String scpPid = "";  
  47.           
  48.         Runtime rt = Runtime.getRuntime();  
  49.           
  50.         //shell命令  
  51.         String str[] = {"/bin/sh""-c","ps -ef | grep manager | grep -v grep |awk '{print $2}'"};  
  52.           
  53.         //String str[] = {""};  
  54.           
  55.         Process pcs;  
  56.         try  
  57.         {  
  58.             agentLog.getLog()  
  59.                     .debug("Run shell: ps -ef | grep manager | grep -v grep |awk '{print $2}'");  
  60.             pcs = rt.exec(str);  
  61.               
  62.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  63.                     pcs.getInputStream()));  
  64.               
  65.             //得到shell的执行结果  
  66.             scpPid = br.readLine();  
  67.               
  68.             //当manager没有运行时,得到shell的返回值的,所以这里要特殊处理一下  
  69.             if(scpPid == null)  
  70.             {  
  71.                 scpPid = "";  
  72.             }  
  73.               
  74.             agentLog.getLog().debug("Get Pid of Manager: " + scpPid);  
  75.               
  76.             try  
  77.             {  
  78.                 pcs.waitFor();  
  79.             }  
  80.             catch (InterruptedException e)  
  81.             {  
  82.                 agentLog.getLog()  
  83.                         .error("Get InterruptedException: processes was interrupted");  
  84.             }  
  85.             br.close();  
  86.               
  87.             pcs.exitValue();  
  88.               
  89.         }  
  90.         catch (IOException e1)  
  91.         {  
  92.             agentLog.getLog().debug("Get IOException!");  
  93.               
  94.             // TODO Auto-generated catch block  
  95.             e1.printStackTrace();  
  96.         }  
  97.           
  98.         return scpPid;  
  99.           
  100.     }  
  101.       
  102.     /** 
  103.      * 通过ps -o etime -p [mangaer pid] 
  104.      * 得到magager的运行时间 
  105.      *  
  106.      * 重点在etime 
  107.      *  
  108.      * HP和HPAT上需要先设置环境变量 UNIX95=1才能使用-o选项。 
  109.      */  
  110.     public static long GetScpRunTime(String Pid)  
  111.     {  
  112.         String str2[] = {"/bin/sh""-c""ps -o etime -p " + Pid};  
  113.           
  114.         String scpRunTime = "";  
  115.           
  116.         Runtime rt = Runtime.getRuntime();  
  117.           
  118.         Process pcs2;  
  119.         try  
  120.         {  
  121.               
  122.             System.out.println("ps -o etime -p " + Pid);  
  123.               
  124.             agentLog.getLog().debug("Run shell: ps -o etime -p " + Pid);  
  125.               
  126.             pcs2 = rt.exec(str2);  
  127.               
  128.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  129.                     pcs2.getInputStream()));  
  130.               
  131.             //当manager进程只有一个时,该命令应该返回两行,第二行是etime的时间  
  132.             String line = new String();  
  133.             while ((line = br.readLine()) != null)  
  134.             {  
  135.                 scpRunTime = line.trim();  
  136.             }  
  137.               
  138.             agentLog.getLog().debug("Get RunTime of Manager: " + scpRunTime);  
  139.               
  140.             try  
  141.             {  
  142.                 pcs2.waitFor();  
  143.             }  
  144.             catch (InterruptedException e)  
  145.             {  
  146.                 agentLog.getLog()  
  147.                         .error("Get InterruptedException: processes was interrupted");  
  148.             }  
  149.             br.close();  
  150.               
  151.             pcs2.exitValue();  
  152.               
  153.         }  
  154.         catch (IOException e1)  
  155.         {  
  156.             agentLog.getLog().debug("Get IOException!");  
  157.               
  158.             // TODO Auto-generated catch block  
  159.             e1.printStackTrace();  
  160.         }  
  161.           
  162.         //转换etime的时间格式,为毫秒  
  163.         return ChangeTimeFormat(scpRunTime);  
  164.     }  
  165.       
  166.     /** 
  167.      * 转换etime的时间格式[[dd-]hh:]mm:ss 
  168.      * 为毫秒 
  169.      */  
  170.     public static long ChangeTimeFormat(String etimeFormat)  
  171.     {  
  172.         String day = "";  
  173.         String hour = "";  
  174.         String minuter = "";  
  175.         String second = "";  
  176.           
  177.         String tempEtimeFormat = etimeFormat;  
  178.           
  179.         if (etimeFormat.indexOf("-") != -1)  
  180.         {  
  181.             day = etimeFormat.substring(0, etimeFormat.indexOf("-"));  
  182.               
  183.             //tempEtimeFormat为[hh:]mm:ss  
  184.             tempEtimeFormat = etimeFormat.substring(etimeFormat.indexOf("-") + 1,  
  185.                     etimeFormat.length());  
  186.               
  187.         }  
  188.           
  189.         //从etimeFormat中得到秒  
  190.         second = tempEtimeFormat.substring(tempEtimeFormat.lastIndexOf(":") + 1,  
  191.                 tempEtimeFormat.length());  
  192.         tempEtimeFormat = tempEtimeFormat.substring(0,  
  193.                 tempEtimeFormat.lastIndexOf(":"));  
  194.           
  195.         minuter = tempEtimeFormat.substring(tempEtimeFormat.lastIndexOf(":") + 1,  
  196.                 tempEtimeFormat.length());  
  197.           
  198.         if (tempEtimeFormat.lastIndexOf(":") != -1)  
  199.         {  
  200.             tempEtimeFormat = tempEtimeFormat.substring(0,  
  201.                     tempEtimeFormat.lastIndexOf(":"));  
  202.               
  203.             hour = tempEtimeFormat;  
  204.         }  
  205.           
  206.         Long dayLong = new Long(0);  
  207.         Long hourLong = new Long(0);  
  208.         Long mimuterLong = new Long(0);  
  209.         Long secondLong = new Long(0);  
  210.           
  211.         if (day.compareTo("") != 0)  
  212.         {  
  213.             dayLong = Long.valueOf(day);  
  214.         }  
  215.         if (hour.compareTo("") != 0)  
  216.         {  
  217.             hourLong = Long.valueOf(hour);  
  218.         }  
  219.           
  220.         mimuterLong = Long.valueOf(minuter);  
  221.         secondLong = Long.valueOf(second);  
  222.           
  223.         //以毫秒为单位  
  224.         long scpRunTime = dayLong.longValue() * 24 * 60 * 60 * 1000  
  225.                 + hourLong.longValue() * 60 * 60 * 1000  
  226.                 + mimuterLong.longValue() * 60 * 1000 + secondLong.longValue()  
  227.                 * 1000;  
  228.           
  229.         agentLog.getLog().debug("Change time to millisecond: " + scpRunTime);  
  230.           
  231.         return scpRunTime;  
  232.           
  233.     }  
  234.       
  235.     public static void main(String args[])  
  236.     {  
  237.         if(isScpRun())  
  238.         {  
  239.             System.out.println("Manger is running");  
  240.   
  241.             String pid = GetScpPid();  
  242.               
  243.             System.out.println("Pid is: " + pid);  
  244.               
  245.             long time = GetScpRunTime(pid);  
  246.               
  247.             System.out.println("Time is: " + time);  
  248.         }  
  249.         else  
  250.         {  
  251.             System.out.println("Manger is not running");  
  252.         }  
  253.           
  254.           
  255.           
  256.     }  
  257. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值