【发布时间】: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 还没有出现,所以它退出了循环?如果是这样,我该如何解决?
【问题讨论】: