Pid subprocess is different from ps output

Why does this pid ( Popen.pid) subprocess have a different meaning from which the command is returned ps?

I noticed this when pscalled both from within python (c subprocess.call()) and from another terminal.

Here is a simple python test file:

#!/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

Typically, the pid reported psis 1 or 2 more than the reported one Popen.pid.

+3
source share
1 answer

Since the command starts with shell=True, the pid returned by the subprocess is the shell process used to run the command.

+4
source

Source: https://habr.com/ru/post/1780293/


All Articles