Question
Asked By – Evan Fosmark
Is there a way to check to see if a pid corresponds to a valid process? I’m getting a pid from a different source other than from os.getpid()
and I need to check to see if a process with that pid doesn’t exist on the machine.
I need it to be available in Unix and Windows. I’m also checking to see if the PID is NOT in use.
Now we will see solution for issue: How to check if there exists a process with a given pid in Python?
Answer
Sending signal 0 to a pid will raise an OSError exception if the pid is not running, and do nothing otherwise.
import os
def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
This question is answered By – mluebke
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0