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?

7
  • What do you mean by 'miniprocesses'? If pid can be found, the program exists and is running, right? Commented May 21, 2014 at 13:08
  • 1
    You can use p.terminate() to stop the child process and p.poll() to check if it is still running. Commented May 21, 2014 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? Commented May 21, 2014 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). Commented May 21, 2014 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 Commented May 21, 2014 at 13:25

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please add an example of how to use p.poll?
you can find an example on this answer: stackoverflow.com/questions/12057794/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.