【问题标题】:Method to execute Linux command fails执行 Linux 命令的方法失败
【发布时间】:2017-09-18 09:52:43
【问题描述】:

我创建了以下方法来执行 Linux 命令:

public void executeGetLogs(){
    try{
        Runtime rt = Runtime.getRuntime();
        String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()};
        Process proc = rt.exec(commands);

        BufferedReader stdInput = new BufferedReader(new 
                InputStreamReader(proc.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
                InputStreamReader(proc.getErrorStream()));

        // read the output from the command
        logger.debug("Standard output from execution of get_logs:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            logger.debug(s);
        }

        // read any errors from the attempted command
        logger.debug("Standard error from execution of get_logs (if any):\n");
        while ((s = stdError.readLine()) != null) {
            logger.debug(s);
        }
    }
    catch(IOException e){
        logger.debug("Execution exception: " + e);
    }
}

该方法似乎开始正常工作,但随后失败。

调试显示以下输出:

2017-04-21 12:27:42,391 DEBUG  Standard output from execution of get_logs:

2017-04-21 12:27:44,360 DEBUG  411 new files found
2017-04-21 12:27:44,363 DEBUG  Downloading files...
2017-04-21 12:27:44,446 DEBUG  Standard error from execution of get_logs (if any):

我希望看到的是

2017-04-21 12:27:44,360 DEBUG  411 new files found
2017-04-21 12:27:44,363 DEBUG  Downloading files...
Downloaded 10 of 447
Downloaded 20 of 447
Downloaded 30 of 447

依此类推,直到已下载 447 个,共 447 个。

我还可以看到没有下载任何内容。

我的命令在终端中运行时运行。

Java 中是否有可能导致它退出?一件事是处理每个 10 块可能需要几秒钟。有可能

while ((s = stdInput.readLine()) != null) {
    logger.debug(s);
}

只看到一个空值,因为 stdInput 还没有出现,所以它退出了循环?如果是这样,我该如何解决?

【问题讨论】:

    标签: java linux command


    【解决方案1】:

    在 java 中执行代码的更好方法是ProcessBuilder。示例:

    //Create the command.
    ProcessBuilder command = new ProcessBuilder(helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword());
    //Will show console output in java console.
    command .inheritIO();
    //Launches de command.
    Process process= command .start();
    

    希望对你有帮助。

    【讨论】:

    • 我会试一试的。谢谢
    • 我使用它时似乎遇到了同样的问题。
    • 好的,这是一个想法。处理输出有一个计数(Downloaded 10 of 447 etc)。我在想如果我读了总数。在这种情况下 447 并循环计数。类似于 while(coint
    • 抱歉没有回答。我看到你必须等待脚本结束。如果您的流程每次迭代都打印到控制台,您应该能够读取流中的每一行(刚刚测试过)。我仍然建议您使用 ProcessBuilder 来纠正来自 Runtime.getRuntime() 的错误,这里有很好的解释:stackoverflow.com/questions/6856028/…。很高兴你解决了你的问题,干得好!
    【解决方案2】:

    找到它的底部。我需要等待该过程结束。这可以通过以下代码完成:

    proc.waitFor();
    

    我的方法现在看起来像这样:

    public void executeGetLogs(){
        try{
            Runtime rt = Runtime.getRuntime();
            String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()};
            Process proc = rt.exec(commands);
    
            BufferedReader stdInput = new BufferedReader(new 
                InputStreamReader(proc.getInputStream()));
    
            try{
                proc.waitFor();
            }
            catch(InterruptedException e){
                logger.debug("Interrupted Exception : " + e);
            }
    
            BufferedReader stdError = new BufferedReader(new 
                InputStreamReader(proc.getErrorStream()));
    
            // read the output from the command
            logger.debug("Standard output from execution of get_logs:\n");
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                logger.debug(s);
            }
    
            // read any errors from the attempted command
            logger.debug("Standard error from execution of get_logs (if any):\n");
            while ((s = stdError.readLine()) != null) {
                logger.debug(s);
            }
        }
        catch(IOException e){
            logger.debug("Execution exception: " + e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 2017-11-12
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多