【问题标题】:Command line proccess read linux in java [duplicate]命令行进程在java中读取linux [重复]
【发布时间】:2018-12-08 10:44:41
【问题描述】:

我正在尝试执行此操作:

    Process p = Runtime.getRuntime().exec("ps -ef | grep bash");

    BufferedReader r =  new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = r.readLine()) != null) {
      System.out.println(line);
    }

想看几行,但它返回Null

如果我只执行"ps -ef",它会返回正确的所有进程。

一般我需要发送关键字到方法,返回工作流程与否

【问题讨论】:

    标签: java linux bash command-line


    【解决方案1】:

    这会起作用

    String[] command =  {"/bin/sh", "-c", "ps -ef | grep bash"};
    Process p = Runtime.getRuntime().exec(command);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2013-12-06
      • 2013-05-27
      • 1970-01-01
      相关资源
      最近更新 更多