1

What I want

I would like to ssh to a remote machine, start a process in the background, and retrieve the pid.

mrocklin@notebook:~$ echo hello &    
[1] 29974
hello

What I tried

I would like to do this same thing with Paramiko

In [1]: import paramiko
In [2]: client = paramiko.SSHClient()
In [3]: client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

In [4]: client.connect('127.0.0.1')
In [5]: stdin, stdout, stderr = client.exec_command('echo hello &')

In [6]: stderr.read()
Out[6]: b''
In [7]: stdout.read()
Out[7]: b'hello\n'

When I do this with a process like sleep, stdout.read() blocks, cementing the fact that this isn't happening in the background.

What I really want

I want to run processes in the background because I want the ability to kill the process based on the PID later if I choose. Is there a better way to go about launching and killing several processes on a remote machine?

MRocklin
  • 55,641
  • 23
  • 163
  • 235
  • 1
    `stdin, stdout, stderr = client.exec_command('sleep 5 & echo $!');print(stdout.readline());print("foo")` would get the pid and show the process is not blocking, it is the python read that is blocking – Padraic Cunningham Mar 05 '16 at 02:18
  • 1
    Perhaps this other SO issue may be useful: http://stackoverflow.com/questions/17560498/running-process-in-the-background – quasiben Mar 05 '16 at 02:33

0 Answers0