How do I check if a pid is running?

Gustavo Niemeyer niemeyer at conectiva.com
Fri May 10 12:36:13 EDT 2002


> >>> def pid_exists(pid):
> ...     try:
> ...         os.kill(pid, 0)
> ...         return 1
> ...     except OSError, err:
> ...         return err.errno == errno.EPERM
[...]
> There may be better ways than this...

I wouldn't classify it as better, but the proc filesystem
usually has directories named with pid numbers. This should
work if you have such proc filesystem (remember, not portable
at all):

def pid_exists(pid):
    return os.path.isdir("/proc/"+str(pid))

-- 
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]





More information about the Python-list mailing list