title: Send signals to remote process with Python
body: <p>There are two machines, where one has a script <code>wait_for_signal.sh</code>, and the second one has a script named <code>controller.py</code>. The code of each script is shown below. </p>
  
  <p>The purpose of the <code>controller.py</code> is to spawn a subprocess that calls the <code>wait_for_signal.sh</code> script through <code>ssh</code>. When the controller needs to exit it needs to send an interrupt to the remote process that runs <code>wait_for_signal.sh</code>.</p>
  
  <p><strong>wait_for_signal.sh</strong></p>
  
  <pre><code>#!/bin/bash
  trap 'break' SIGINT
  trap 'break' SIGHUP
  
  echo "Start loop"
  
  while true; do
    sleep 1
  done
  
  echo "Script done"
  </code></pre>
  
  <p><strong>controller.py</strong></p>
  
  <pre><code>import os
  import signal
  import subprocess
  
  remote_machine = user@ip
  remote_path = path/to/script/
  
  remote_proc = subprocess.Popen(['ssh', '-T', remote_machine, 
                                 './' + remote_path + 'wait_for_signal.sh'], 
                                 shell=False, stdout=subprocess.PIPE, 
                                 stderr=subprocess.PIPE)
  
  # do other stuff
  
  os.kill(remote_proc.pid, signal.SIGINT)
  </code></pre>
  
  <p>Currently, the signal send is to the process that started the ssh connection on the local machine and not the remote machine. This causes the local process to stop but the remote process continues to execute.</p>
  
  <p>How does ssh work and what type of signals does it send to the remote machine when it is stopped? How can I send the appropriate signal to the remote process, which was started by the ssh connection?</p>