Given PID, make sure a process is running on Unix?

François Pinard pinard at iro.umontreal.ca
Sat Jul 6 19:25:47 EDT 2002


[VanL]

> I am trying to get write a python script that on Unix reads a PID from
> a file and, if that process is still running, exits.  If the process is
> not running, the process would be started.  [...] Any help?

You could work along the lines of:

    try:
        os.kill(PID, 0)
    except OSError:
        print PID, 'is not running'
    else:
        print PID, 'is running'

Killing a process with signal 0 never kills it. :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list