Skip to content Skip to sidebar Skip to footer

Check If PID Exists On Windows With Python Without Requiring Libraries

Is there a way to check if a PID exists on Windows with Python without requiring libraries? How to?

Solution 1:

This is solved with a little cup of WINAPI.

def pid_running(pid):
    import ctypes
    kernel32 = ctypes.windll.kernel32
    SYNCHRONIZE = 0x100000

    process = kernel32.OpenProcess(SYNCHRONIZE, 0, pid)
    if process != 0:
        kernel32.CloseHandle(process)
        return True
    else:
        return False

Solution 2:

This works on my system..

>>> import subprocess
>>> out = subprocess.check_output(["tasklist","/fi","PID eq 1234"]).strip()
>>> if out == "INFO: No tasks are running which match the specified criteria.":
...   print "No such PID :D"
...
No such PID :D

Post a Comment for "Check If PID Exists On Windows With Python Without Requiring Libraries"