读书人

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

发布时间: 2012-10-29 10:03:53 作者: rapoo

用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;
}

读书人网 >UNIXLINUX

热点推荐