码迷,mamicode.com
首页 > Windows程序 > 详细

Windows与Linux获取进程集合的方法

时间:2017-06-23 11:43:14      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:判断   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;
        }

  

Windows与Linux获取进程集合的方法

标签:判断   pid   pattern   system   进程   cep   readline   run   get   

原文地址:http://www.cnblogs.com/ginkgo5198/p/7068982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!