4

Why is it that the subprocess pid (Popen.pid) has different value from that the ps command returns?

I've noticed this when ps called both from inside python (with subprocess.call()) and from another terminal.

Here's a simple python file to test:

#!/usr/bin/python3
'''
Test subprocess termination
'''

import subprocess

command = 'cat'

#keep pipes so that cat doesn't complain
proc = subprocess.Popen(command,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    stdin=subprocess.PIPE,
                    shell=True)

print('pid = %d' % proc.pid)
subprocess.call("ps -A | grep -w %s" % command,
                    shell=True)

proc.terminate()
proc.wait()             # make sure its dead before exiting pytyhon

Usually the pid reported by ps is 1 or 2 more than that reported by Popen.pid.

Matthew
  • 393
  • 4
  • 9
  • It worked on my mac. Are you sure that you are not watching `grep command` pid in `ps` output? On my mac `ps -A | grep` lists three processes - `cat`, `/bin/sh -c ps -A | grep -w cat`, `grep -w cat`. – khachik Dec 14 '10 at 21:03
  • 2
    I just realized - maybe `Popen.pid` is the shell process id and ps|grep report the pid of `cat`? – khachik Dec 14 '10 at 21:06
  • Yep. It was `shell=True`. Someone had posted the answer, but it doesn't seem to be here anymore... To whomever it was, please repost so I can accept. – Matthew Dec 14 '10 at 21:11
  • As an interesting aside, the pid referencing the spawned shell may also interfere with terminate() and kill(). Or at least it appears to because the signal is sent to the shell instead of the desired subprocess. – Matthew Dec 14 '10 at 23:01

1 Answers1

5

Because the command is run with shell=True, the pid returned by subprocess is that of the shell process used to run the command.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 1
    Can you expand on this? I'm trying to open an ssh tunnel using subprocess and run into the same problem as the OP, where the `process.pid` is not the actual value, but running the function with `shell=True` or `shell=False` doesn't seem to change anything. – rer Aug 15 '18 at 19:12