I'm writing a program in python which calls another program via the subprocess.Popen
command.
The program being called launches a GUI that the user interacts with, but before that it goes through some services to authenticate the user.
With that being said, I'm trying to figure out a way to know when the user exits out of that GUI. Basically, I want to wait until the GUI is exited, and then continue in my program.
My problem is that since the program I make a call to goes through those services before the GUI is actually launched, it seems like the program I make a call to ends and then spawns a new process which is the GUI, and then 'm not able to wait on the pid because the pid I have as already terminated.
I have tried this:
p = subprocess.Popen('/user/sbin/startprogramtolaunchgui')
p.wait()
printf 'done'
But 'done' gets printed right away, not after the GUI is exited out of. Also, when I run the command
ps -ef
The program 'startprogramtolaunchgui' I called is not in the process list. BUT, I see the gui that it launched in the process list (the one that I want to monitor)
EDIT:
I came up with this:
def isRunning(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
p = subprocess.Popen('/user/sbin/startprogramtolaunchgui')
time.sleep(5)
temp = subprocess.check_output(['pgrep', 'gui']) #get pid of GUI process
while(isRunning(int(temp))):
pass
print 'GUI CLOSED'
It works...but is this really an okay way to do it??