title: Wait for a process to end that is created by a process called
body: <p>I'm writing a program in python which calls another program via the <code>subprocess.Popen</code> command. </p>
  
  <p>The program being called launches a GUI that the user interacts with, but before that it goes through some services to authenticate the user.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>p = subprocess.Popen('/user/sbin/startprogramtolaunchgui')
  p.wait()
  printf 'done'
  </code></pre>
  
  <p>But 'done' gets printed right away, not after the GUI is exited out of. Also, when I run the command </p>
  
  <pre><code>ps -ef
  </code></pre>
  
  <p>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)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I came up with this:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>It works...but is this really an okay way to do it??</p>