title: python subprocess run a remote process in background and immediately close the connection
body: <p>The task is to use python to run a remote process in background and immediately close the ssh session.</p>
  
  <p>I have a remote script name 'start' under server:PATH/, the start script does nothing but lunch a long-live background program. 'start' script which has one line:</p>
  
  <pre><code>nohup PATH/Xprogram &amp;
  </code></pre>
  
  <p>When I use python subprocess module to call my remote 'start' script, it does start OK. But the issue is: it seems the SSH connection is persist, meaning I am getting stdout from the remote Xprogram (since it is a long live program which has output to stdout). Does this indicating ssh connection is still there ?</p>
  
  <p>All I need is call the start script without blocking and forget about it (leave the long-live program running, close ssh, release resources).</p>
  
  <p>my python function call looks like this:</p>
  
  <pre><code>ret = subprocess.Popen(["ssh", "xxx@servername", "PATH/start"])
  </code></pre>
  
  <p>if I use <code>ret.terminate()</code> after the command, it then will kill the long-live program too.
  I have also tried spur module. basically the same thing. </p>
  
  <p>=====update====</p>
  
  <p>@Dunes' answer solves the problem. Based on his answer, I did more digging and found <a href="https://unix.stackexchange.com/questions/30400/execute-remote-commands-completely-detaching-from-the-ssh-connection">this link</a> very helpful. 
  My understanding of this is: basically, if any file descriptor is still held by your process (e.g. stdout held by my XProgram), then SSH session won't exit. However redirect stdout/stderr to NULL effectively close those file descriptor and let SSH session exit normally.</p>
  
  <p><strong>solution</strong>  </p>
  
  <pre><code>ret = subprocess.Popen(["ssh", "xxx@servername", "PATH/start &gt;dev/null 2&gt;&amp;1"])
  </code></pre>