2

I have an environment variable that allows a suite of applications to run under certain conditions, and the applications cannot run with the environment variable off.

My python script uses

p = subprocess.Popen(cmdStr3,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

to open the subprocess.

To test if the application is running, I have tried

try:
    os.kill(pid, 0)
    return True
except OSError:
    return False

and also checking p.returncode. However, these always return true, because even if the application doesn't pull up on the screen, there are mini processes of ~1 MB that still run without the application fully running, so the os sees these and returns true. Is there a way around this?

Another issue is that os.kill doesn't work, the only way I have found to terminate the application is os.killpg at the end.

What I've learned from the comments is that what actually happens is that the subprocess I call is a starter that calls a child which is another application. My subprocess always runs, but with the environment variable off, the child application does not run. Is there a way to see if the child is running?

user3632833
  • 71
  • 1
  • 2
  • 4
  • What do you mean by 'miniprocesses'? If `pid` can be found, the program exists and is running, right? –  May 21 '14 at 13:08
  • 1
    You can use `p.terminate()` to stop the child process and `p.poll()` to check if it is still running. – Hans Then May 21 '14 at 13:09
  • @Evert you're right. I guess what actually happens is that the subprocess I call is a starter that calls a child which is another application. My subprocess always runs, but with the environment variable off, the child application does not run. Is there a way to see if the child is running? – user3632833 May 21 '14 at 13:15
  • Sounds like you want something like `ps`, assuming those sub-child processes have a unique name. Otherwise, if the starter process doesn't keep track of the child, I think all bets are off (you could search for orphan processes, but there may be plenty). –  May 21 '14 at 13:21
  • I will look into `ps`. For all the cases I've tested, the child application always has a process ID that is one more than the subprocess. Will this always be the case or is this a foolish way to check – user3632833 May 21 '14 at 13:25
  • Well let me tell you, it's foolish. – user3632833 May 21 '14 at 13:44
  • It seems your question is: how to find out whether a **grandchild** process is alive. Update your question, to make it clear that `p.poll()` that reports whether a **child** process is alive is not applicable in your case (because your comments suggest that the *child* continues to run whether grandchild is alive or not). – jfs May 21 '14 at 17:46

1 Answers1

2

you can use p.poll() to check if the process is still running.

Another issue is that os.kill doesn't work, the only way I have found to terminate the application is os.killpg at the end.

If you want to kill a process launched using subprocess, you can use p.terminate().

Finally, if you want to wait until the process child dies, you can use p.wait().

zmo
  • 24,463
  • 4
  • 54
  • 90
  • Can you please add an example of how to use p.poll? – Don Smith Nov 10 '22 at 19:21
  • you can find an example on this answer: https://stackoverflow.com/questions/12057794/python-using-popen-poll-on-background-process/12058609#12058609 ☺ – zmo Nov 14 '22 at 15:54