I want to run a process on a remote machine and I want it to get terminated when my host program exits.
I have a small test script which looks like this:
ssh = paramiko.SSHClient()
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
When the SSH connection is closed it'll not kill the running command on remote host.
The easiest solution is:
ssh.exec_command('python /home/me/loop.py', get_pty=True) # ... do something ... ssh.close()
Then when the SSH connection is closed, the pty (on remote host) will also be closed and the kernel (on remote host) will send the
SIGHUP signal to the remote command. By default
SIGHUP will terminate the process so the remote command will be killed.
According to the APUE book:
SIGHUP is sent to the controlling process (session leader) associated with a controlling terminal if a disconnect is detected by the terminal interface.