I am opening a program using subprocess.Popen
import subprocess
prog = subprocess.Popen(['myprog', args], shell=True)
I want to monitor the process 'myprog' and wait untill it terminates before my code continues.
The problem which I am facing is prog.pid
is the PID of the shell and not 'myprog' and I have no idea how to obtain the PID of 'myprog'. I tried to use psutil
to find the child processes of prog.pid
:
parent = psutil.Process(prog.pid)
children = parent.children(recursive=True)
but an empty list is returned.
Unfortunately I am not able to start 'myprog' without the shell. When I run the command with shell=False
I receive the following error message:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have search the web high and low but all promising leads are for non-Windows users.
Your atom
command is probably a .cmd
file which launches an executable, which explains the need for shell=True
You won't go to the extend of re-coding the .bat file in python, BUT you don't need the executable PID either.
If the executable ends, the batch file ends, so forget about monitoring prog.pid
, just monitor prog
using poll
(in the same python script of course).
The poll
method allows to check if the process has ended (if it has, it returns the exit code, if it hasn't it returns None
), and this is non-blocking so you could do that anytime you want:
if prog.poll() is not None:
# process has ended
print("ended")
Let's remind the blocking wait of doing that:
return_code = prog.wait()
But in your case, the atom.cmd
command seems to start the process in background, making it impossible to monitor without the PID.
A workaround in that case is to make a copy of atom.cmd
, remove the start
prefix to run the executable in foreground and run the copy instead.
(or since the cmd
calls another atom.cmd
file as you commented, locate the file app-1.13.0\resources\cli\atom.cmd
from atom.cmd
original directory and remove the start
prefix there)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With