0

A subprocess is being created that requires use of the internal cmd.exe command start, so a shell is required.

>>> p = subprocess.Popen(['start', 'MCC', '/wait'], shell=True)

The 'MCC' takes place of the title parameter of the start command, and tells the Windows Console Host to load the settings for the console from the registry at HKCU\Console\MCC to open the customized console. Once opened, using the Popen interface, the console will remain after a call to terminate:

>>> p.poll()
>>> p.terminate()
>>> p.poll()
1

My initial diagnosis is that the call to terminate is terminating the subprocess correctly, but its "invisible", and the console that is showing is another process created by start that is not directly accessible from the variable p. I have been successful in terminating the program and closing the console but by running a secondary subprocess:

>>> pp = subprocess.Popen(['taskkill', '/f', '/pid', f'{p.pid}', '/t'])

While it works and is documented, ideally I'd like to avoid invoking another process that could yield zombie issues; thereby leaving me wondering how to terminate a subprocess p and any children it may have created.

For reference I have reviewed the following:

  1. How to terminate a python subprocess launched with shell=True: Linux resolution, gave me the taskkill idea

Environment details:

  • Windows 10
  • CPython 3.6.0
pstatix
  • 3,422
  • 3
  • 13
  • 31

0 Answers0