views:

471

answers:

2

I have a process id in Python. I know I can kill it with os.kill(), but how do I check if it is alive ? Is there a built-in function or do I have to go to the shell?

+7  A: 

Use subprocess module to spawn process. There is proc.poll() - function it returns Null if process is still alive otherwise it returns process returncode.

http://docs.python.org/library/subprocess.html

Mihail
+7  A: 

os.kill does not kill processes, it sends them signals (it's poorly named).

If you send signal 0, you can determine whether you are allowed to send other signals. An error code will indicate whether it's a permission problem or a missing process.

See man 2 kill for more info.

Also, if the process is your child, you can get a SIGCHLD when it dies, and you can use one of the wait calls to deal with it.

Dustin