title: paramiko: automatically terminate remotely started processes
body: <p>I want to run a process on a remote machine and I want it to get terminated when my host program exits.</p>
  
  <p>I have a small test script which looks like this:</p>
  
  <pre><code>import time
  while True:
      print('hello')
      time.sleep(1)
  </code></pre>
  
  <p>and I start this process on a remote machine via a script like this one:</p>
  
  <p>import paramiko</p>
  
  <pre><code>ssh = paramiko.SSHClient()
  ssh.load_system_host_keys()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect('my_machine', username='root', key_filename='some_key')
  _in, _out, _err = ssh.exec_command('python /home/me/loop.py')
  time.sleep(5)  # could do something with in/out/err
  ssh.close()
  </code></pre>
  
  <p>But my problem is that the started process keeps running even after I've closed the Python process which started the SSH connection.</p>
  
  <p>Is there a way to force closing the remote session and the remotely started process when the host process gets terminated?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p><a href="https://stackoverflow.com/questions/7734679/paramiko-and-exec-command-killing-remote-process">This question</a> sounds similar but there is no satisfying answer. I tried to set the <code>keepalive</code> but with no effect:</p>
  
  <pre><code>ssh.get_transport().set_keepalive(1)
  </code></pre>