title: Opening a process with Popen and getting the PID
body: <p>I'm working on a nifty little function:</p>
  
  <pre><code>def startProcess(name, path):
      """
      Starts a process in the background and writes a PID file
  
      returns integer: pid
      """
  
      # Check if the process is already running
      status, pid = processStatus(name)
  
      if status == RUNNING:
          raise AlreadyStartedError(pid)
  
      # Start process
      process = subprocess.Popen(path + ' &gt; /dev/null 2&gt; /dev/null &amp;', shell=True)
  
      # Write PID file
      pidfilename = os.path.join(PIDPATH, name + '.pid')
      pidfile = open(pidfilename, 'w')
      pidfile.write(str(process.pid))
      pidfile.close()
  
      return process.pid
  </code></pre>
  
  <p>The problem is that <code>process.pid</code> isn't the correct PID. It seems it's always 1 lower than the correct PID. For instance, it says the process started at 31729, but <code>ps</code> says it's running at 31730. Every time I've tried it's off by 1. I'm guessing the PID it returns is the PID of the <strong>current</strong> process, not the started one, and the new process gets the 'next' pid which is 1 higher. If this is the case, I can't just rely on returning <code>process.pid + 1</code> since I have no guarantee that it'll always be correct.</p>
  
  <p>Why doesn't <code>process.pid</code> return the PID of the new process, and how can I achieve the behaviour I'm after?</p>