从Java运行shell脚本
我试图使用commons exec包运行一些Java shell脚本,并使用PumpStreamHandler清除STDOUT和STDERR缓冲区。大多数脚本运行正常,没有任何问题,但有些脚本挂起 特别是那些需要一些时间才能返回的脚本。我的猜测是PumpStramHandle可能正在读取流的末尾,因为有一段时间流中并没有任何内容,然后缓冲区就会填满从Java运行shell脚本,java,shell,exec,apache-commons-exec,Java,Shell,Exec,Apache Commons Exec,我试图使用commons exec包运行一些Java shell脚本,并使用PumpStreamHandler清除STDOUT和STDERR缓冲区。大多数脚本运行正常,没有任何问题,但有些脚本挂起 特别是那些需要一些时间才能返回的脚本。我的猜测是PumpStramHandle可能正在读取流的末尾,因为有一段时间流中并没有任何内容,然后缓冲区就会填满 有没有更好的方法解决这个问题?提取正在执行的脚本/命令,并在shell中自己运行。当运行某些其他语言(C、C++、Python java等)执行的事
有没有更好的方法解决这个问题?提取正在执行的脚本/命令,并在shell中自己运行。当运行某些其他语言(C、C++、Python java等)执行的事情时,事情开始出错,这应该是第一步。p>
你会发现发生了各种各样的事情。停止并提示输入错误的脚本(很大的挂起源)、未正确解析的错误、seg错误、未找到文件。要扩展关于直接运行命令进行测试的第一个答案,您可以使用一个简单的脚本来测试您的假设,该脚本在返回输出之前休眠一段时间。如果你 无法测试您的命令,请测试您的想法
#!/bin/bash
sleep 60;
echo "if you are patient, here is your response"
这不是最好的解决办法。但我需要什么呢 用法:
Process p = Runtime.getRuntime().exec("Command");
OSCommandLogger logger = new OSCommandLogger(p, "Command", log);
// Start the thread using thread pool
threadExec.executeRunnable(logger);
int exitValue = p.waitFor(); // Wait till the process is finished
// Required to stop the logger threads
logger.stopLoggers();
logger.interrupt();
我承认你是对的。但在这种情况下,这真的不是一个选择。
Process p = Runtime.getRuntime().exec("Command");
OSCommandLogger logger = new OSCommandLogger(p, "Command", log);
// Start the thread using thread pool
threadExec.executeRunnable(logger);
int exitValue = p.waitFor(); // Wait till the process is finished
// Required to stop the logger threads
logger.stopLoggers();
logger.interrupt();