title: golang exec background process and get its pid
body: <p><strong>Situation:</strong></p>
  
  <p>I want to run a command that puts itself into the background. If it makes it more possible, then I'll run the command in foreground and bring it into the background by myself. </p>
  
  <p><strong>Question:</strong></p>
  
  <p>When the process runs in background: <em>how can I get it's <code>pid</code> using Go?</em> </p>
  
  <p><strong>I tried the following:</strong></p>
  
  <pre><code>cmd := exec.Command("ssh", "-i", keyFile, "-o", "ExitOnForwardFailure yes", "-fqnNTL", fmt.Sprintf("%d:127.0.0.1:%d", port, port), fmt.Sprintf("%s@%s", serverUser, serverIP))
  cmd.Start()
  pid := cmd.Process.Pid
  cmd.Wait()
  </code></pre>
  
  <p>This returns instantly and leaves <code>ssh</code> running in the background. But it's <code>pid</code> is not the <code>pid</code> of the running <code>ssh</code> process. Moreover, it's the <code>pid</code> of the parent <code>ssh</code> process before it forked and backgrounded itself.</p>