How to check if a process is still running using Python on Linux? [duplicate] How to check if a process is still running using Python on Linux? [duplicate] python python

How to check if a process is still running using Python on Linux? [duplicate]


Mark's answer is the way to go, after all, that's why the /proc file system is there. For something a little more copy/pasteable:

 >>> import os.path >>> os.path.exists("/proc/0") False >>> os.path.exists("/proc/12") True


on linux, you can look in the directory /proc/$PID to get information about that process. In fact, if the directory exists, the process is running.


It should work on any POSIX system (although looking at the /proc filesystem, as others have suggested, is easier if you know it's going to be there).

However: os.kill may also fail if you don't have permission to signal the process. You would need to do something like:

import sysimport osimport errnotry:    os.kill(int(sys.argv[1]), 0)except OSError, err:    if err.errno == errno.ESRCH:        print "Not running"    elif err.errno == errno.EPERM:        print "No permission to signal this process!"    else:        print "Unknown error"else:    print "Running"