In daemon.py/daemon_stop, there is snippet as follows:
try:
# query for the pid
os.kill(pid, 0)
except OSError as e:
if e.errno == errno.ESRCH:
break
The usual way to check if a process is still running is to kill() it with signal '0'. It does nothing to a running job and raises an OSError exception with errno=ESRCH if the process does not exist. And kill(pid, 0) can also raise EPERM (access denied) in which case that obviously means a process exists.
def pid_exists(pid):
"""Check whether pid exists in the current process table. UNIX only.
"""
if pid < 0:
return False
if pid == 0:
# According to "man 2 kill" PID 0 refers to every process
# in the process group of the calling process.
# On certain systems 0 is a valid PID but we have no way to know that in a portable fashion.
raise ValueError('invalid PID 0')
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# ESRCH == No such process
return False
elif err.errno == errno.EPERM:
# EPERM clearly means there's a process to deny access to
return True
else:
# According to "man 2 kill" possible error values are (EINVAL, EPERM, ESRCH)
raise
else:
return True
Note: os.kill(pid, 0) can only work on linux and unix, but not windows. Here is the issue about why os.kill on Windows should not accept zero as signal.
In
daemon.py/daemon_stop
, there is snippet as follows:The usual way to check if a process is still running is to kill() it with signal '0'. It does nothing to a running job and raises an OSError exception with
errno=ESRCH
if the process does not exist. And kill(pid, 0) can also raiseEPERM
(access denied) in which case that obviously means a process exists.Note: os.kill(pid, 0) can only work on linux and unix, but not windows. Here is the issue about why os.kill on Windows should not accept zero as signal.
Ref
How to check if there exists a process with a given pid in Python?
What is the easiest way to see if a process with a given pid exists in Python?
os.kill on Windows should accept zero as signal