标签:判断 pid pattern system 进程 cep readline run get
Windows:
List<String> tasklist=new ArrayList<String>();
try {
Process process = Runtime.getRuntime().exec(cmdstr);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (in.hasNextLine()) {
String p = in.nextLine();
if (p.contains("java.exe")) {
p = p.replaceAll("\\s+", ",");
String[] arr = p.split(",");
tasklist.add(arr[1]);//此处要进行判断
}
}
in.close();
Centos:
public static List<String> getPidFromCentos(){
List<String> tasklist=new ArrayList<String>();
Process process = null;
BufferedReader br = null;
try {
String[] cmd ={"sh","-c","ps -ef | grep java"};
process = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(
process.getInputStream()), 1024);
String line = null;
Pattern pattern = Pattern.compile("\\S*\\s*([0-9]*).*");
Matcher matcher = null;
while ((line = br.readLine()) != null) {
matcher = pattern.matcher(line);
if (matcher.find()) {
tasklist.add(matcher.group(1));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (process != null) {
int retval = 0;
try {
retval = process.waitFor();
System.out.println("retval = " + retval);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return tasklist;
}
标签:判断 pid pattern system 进程 cep readline run get
原文地址:http://www.cnblogs.com/ginkgo5198/p/7068982.html