public String execCmd(String command) throws Exception {
initChannelExec();
log.info("execCmd command - > {}", command);
channel.setInputStream(System.in);
channelExec.setCommand(command);
channel.connect();
StringBuilder sb = new StringBuilder(16);
try (InputStream in = channel.getInputStream();
InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr)) {
String buffer;
while ((buffer = reader.readLine()) != null) {
log.info(buffer);
sb.append("\n").append(buffer);
}
log.info("execCmd result - > {}", sb);
return sb.toString();
}
}
import time
print("hello world start")
time.sleep(10)
print("hello world end.")
log.info(buffer);这句打印hello world start 和hello world end是同时打印出来的,我想要先打印第一行,等10s再打印第二行,请问怎么实现
exec方式为非交互式,就是执行一条命令,等这个命令所有都执行完了,才返回,
shell方式为交互式,可以实时返回服务器的输出,并等待再次输入,但是这块我也还在研究
我的远程脚本也是会执行一段时间,并且往标准输出里面打了日志,我也很想能实时记下这些日志,而不是执行完了一股脑给返回来,并不能还原远程脚本执行的现场情况,后续有进展了,欢迎交流
用shell管道是可以实时返回了,但是会偶尔出现一直hang住的情况,然后等ssh自动超时断开,这期间就一直跑着,也不知道执行没有,没有任何结果输出