执行 Linux 命令的方法失败
Method to execute Linux command fails
我创建了以下方法来执行 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 中执行代码的更好方法是 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();
希望对您有所帮助。
追根究底。我需要等待进程结束。这可以通过以下代码完成:
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);
}
}
我创建了以下方法来执行 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 中执行代码的更好方法是 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();
希望对您有所帮助。
追根究底。我需要等待进程结束。这可以通过以下代码完成:
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);
}
}