paramiko SSH exec_command(shell脚本)在完成之前返回

Lar*_*rry 15 python paramiko

我使用paramiko从远程Linux机器启动shell脚本.启动shell脚本并执行命令make -j8.但是exec_command在make完成之前返回.

如果我在本地计算机上启动脚本,它会正确执行.

有人能解释一下这种行为吗?

def*_*ode 26

您需要等待应用程序完成,exec_command不是阻塞调用.

print now(), "before call"
stdin, stdout, sterr = ssh.exec_command("sleep(10)")
print now(), "after call"
channel = stdout.channel
print now(), "before status"
status = channel.recv_exit_status()
print now(), "after status"
Run Code Online (Sandbox Code Playgroud)

  • 那些人()的目的是什么?似乎他们都会在一个接一个地跑得很快?你是说等待应用程序完成,你需要'while not channel.recv_exit_status():'等待循环?如果是这样,一旦recv_exit_status()返回True,如果stdout上有数据,是否保证stdout.channel.recv_ready()为True?似乎有时情况并非如此. (2认同)