在等待命令完成的同时,必须连续读取输出。否则,如果命令产生足够的输出以填充输出缓冲区,则该命令将挂起,等待缓冲区被消耗,这将永远不会发生。这样您就陷入僵局。
以下示例在监视命令状态的同时连续读取stdout和stderr。它基于官方的JSch
exec.java示例(仅添加了stderr的阅读)。
ChannelExec channel = (ChannelExec)session.openChannel("exec");channel.setCommand( "for((i=1;i<=10000;i+=2)); do echo "Long output - $i"; done ; " + "echo error output >&2");InputStream commandOutput = channel.getExtInputStream();StringBuilder outputBuffer = new StringBuilder();StringBuilder errorBuffer = new StringBuilder();InputStream in = channel.getInputStream();InputStream err = channel.getExtInputStream();channel.connect();byte[] tmp = new byte[1024];while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; outputBuffer.append(new String(tmp, 0, i)); } while (err.available() > 0) { int i = err.read(tmp, 0, 1024); if (i < 0) break; errorBuffer.append(new String(tmp, 0, i)); } if (channel.isClosed()) { if ((in.available() > 0) || (err.available() > 0)) continue; System.out.println("exit-status: " + channel.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) { }}System.out.println("output: " + outputBuffer.toString());System.out.println("error: " + errorBuffer.toString());channel.disconnect();如果在
while (!channel.isClosed()) {}之后添加channel.connect();,您会看到
ishell
for循环中有足够大的空间(在我的环境中,10000就足够了),循环永远不会结束。



