首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

用JAVA查看Linux的历程情况,并获取PID

2012-10-29 
用JAVA查看Linux的进程情况,并获取PID//判断本地进程状态//command为要查看的进程的启动命令,若存在可获得

用JAVA查看Linux的进程情况,并获取PID
//判断本地进程状态

//command为要查看的进程的启动命令,若存在可获得PID,然后可以执行KILL以便杀掉,呵呵。command也可以为其他Linux上的执行命令,但就不是查看进程了,可以改造为查看命令执行结果(在while处更改)
public static boolean SoftStatus(String command){
  boolean re=false;
  try{
   command="ps -ef";
   Process child = Runtime.getRuntime().exec(command);
   BufferedReader ins = new BufferedReader(new InputStreamReader(child.getInputStream()));
         String c=null;
         while ((c = ins.readLine()) != null) {
          //System.out.println("PID:"+c.substring(10,14));
          if(c.indexOf(command)>0) System.out.println(c);
         }
         ins.close();
 
   Process child = Runtime.getRuntime().exec(command);
   BufferedReader ins = new BufferedReader(new InputStreamReader(child.getInputStream()));
         String c=null;
         while ((c = ins.readLine()) != null) {
          System.out.println("PID:"+c.substring(10,14));
          if(c.indexOf(command)>0) System.out.println(c);
         }
         ins.close();
  }catch(Exception e){
   System.out.println(e.toString());
  }
 
  System.out.println("SoftStatus() check "+command+" over");
  return re;
}

热点排行