title: Why does ssh wait for my subshells without -t, and kill them with -t?
body: <p>I have a bash script start.sh which looks like this:</p>
  
  <pre><code>for thing in foo bar; do
      {
          background_processor $thing
          cleanup_on_exit $thing
      } &amp;
  done
  </code></pre>
  
  <p>This does what I want: I run start.sh, it exits with code 0, and the two subshells run in the background. Each subshell runs <code>background_processor</code>, and when that exits, it runs <code>cleanup_on_exit</code>. This works even if I exit the terminal from which I originally ran start.sh (even if that was an ssh connection).</p>
  
  <p>Then I tried this:</p>
  
  <pre><code>ssh user@host "start.sh"
  </code></pre>
  
  <p>This works, except that after <code>start.sh</code> has exited, ssh apparently also waits for the subshells to exit. I don't really understand why. Once <code>start.sh</code> exits, the subshells become children of pid 1, and they are not even assigned with a tty... so I can't understand how they are still associated with my ssh connection.</p>
  
  <p>I later tried this:</p>
  
  <pre><code>ssh -t user@host "start.sh"
  </code></pre>
  
  <p>Now the processes have an assigned pseudo-tty. Now, I find that ssh does exit as soon as <code>start.sh</code> exits, but it also kills the child processes.</p>
  
  <p>I guessed that the child processes were being sent SIGHUP in the latter case, so I did this:</p>
  
  <pre><code>ssh -t user@host "nohup start.sh"
  </code></pre>
  
  <p>That actually works! So, I have a solution to my practical problem, but I would like to grasp the subtleties of the SIGHUP/tty stuff here.</p>
  
  <p>In summary, my questions are:</p>
  
  <ol>
  <li>Why does ssh (without -t) wait for the child processes even after <code>start.sh</code> exits, even though they have parent pid 1?</li>
  <li>Why does ssh (with -t) kill the child processes, apparently with a SIGHUP, even though that does not happen when I run them from a terminal and log out of that terminal?</li>
  </ol>