19

I launch a shell script from a remote Linux machine using Paramiko. The shell script is launched and execute a command make -j8. However the exec_command returns before the completion of the make.

If I launch the script on the local machine it executes correctly.

Could someone explain me this behaviour?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Larry
  • 191
  • 1
  • 1
  • 3

1 Answers1

29

You need to wait for application to finish, exec_command isn't a blocking call.

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"
deft_code
  • 57,255
  • 29
  • 141
  • 224
  • 1
    weirdly, using stdout.channel.recv_exit_status() blocks my code forever – sliders_alpha Mar 04 '16 at 10:51
  • @sliders_alpha Indeed, while this answer is in principle correct, this simple implementation will hang/deadlock, if the command's output is sufficiently large. See [Paramiko ssh die/hang with big output](https://stackoverflow.com/q/31625788/850848). – Martin Prikryl Apr 06 '21 at 07:44