2

I am working on a python script that connects to a remote linux machine using paramiko. After the connection is established, I want to run a command on remote machine. This command first creates few files on the remote machine and then do some operations on those files (it may take from 1 sec to 1 min to 10min for the command execution). Next step, I am transporting those files on my local machine using

paramiko.SFTPClient.from_transport(transport).get(filename)

This command is working fine and collecting the files. But the problem is it doesnt wait until all the operations on the files is complete. As a result, I am not getting proper data. Can anyone please help me here?

I found the below commands for this purpose,

stdin, stdout, stderr = ssh_conn_ssh.exec_command(create_and_operate_on_files)

(Note: stdout, stderr etc are just given for namesake. I dont bother about their output)


But this command sh_conn_ssh.exec_command(create_and_operate_on_files) doesnt wait for the command create_and_operate_on_files to get completed. And as a result, I am getting files while they are still being operated upon.

Patrick
  • 4,186
  • 9
  • 32
  • 45
Aakash Goyal
  • 1,051
  • 4
  • 12
  • 44
  • try `sys.open[]` which is wait untill process done – GLHF Jan 21 '15 at 05:31
  • can you please tell where exactly to use this command? – Aakash Goyal Jan 21 '15 at 05:36
  • Your question was `p=subprocess.Popen(create_files_and_operate_on_themstdin,stdout=subprocess.PIPE stdout,stderr=subprocess.STDOUT)` with this. Now it's gone. Why? Use that instead of this – GLHF Jan 21 '15 at 06:04
  • I was searching for solution. subprocess was not executing only. The present one atleast executes without throwing error on console. Thats why modified. Sorry for the trouble. – Aakash Goyal Jan 21 '15 at 06:06
  • @howaboutNO: Even if you have an answer for that `subprocess.Popen`, please tell. I will revert back the changes. – Aakash Goyal Jan 21 '15 at 06:08

1 Answers1

0

exec_command() in Paramiko actually does wait for the command to finish. It looks like your command is not blocking; perhaps it runs in the background intrinsically? If you try running exec_command("sleep 10") you should see it does not return control to your local Python until 10 seconds have passed.

If you cannot make the remote program block until finished, you will need to implement some other mechanism to check if it has completed--such as waiting for the files to be available which you can do using Paramiko's stat() function, as described here: Check whether a path exists on a remote host using paramiko

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436