Adrian
Adrian

Reputation: 2875

How do I know the remote process is running, or complete?

I'm using the Python library Paramiko to run a command over ssh on another server. The problem I'm facing is that the SSHClient.exec_command() call returns immediately, sending me stdin, stdout, and stderr and giving me no other way I can see to tell if the process is still running or not. I thought that I might try monitoring to see if the streams it returns are still open, but I can't find any way to do this except by trying to read from stdout or stderr, or write to stdin and waiting to receive a ValueError. Can anyone tell me of something I've missed that should work instead?

Upvotes: 1

Views: 2387

Answers (1)

Adrian
Adrian

Reputation: 2875

Thanks to advice from @fixxxer I found what I needed to know. My test code now looks like this:

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('localhost', username='user', password='password')

transport = ssh.get_transport()
channel = transport.open_session()
channel.exec_command('./exec_test.py')

status = channel.recv_exit_status()

This works marvellously. It blocks until the command is finished, then allows me to continue.

Upvotes: 1

Related Questions