Created
June 24, 2014 17:18
-
-
Save drgarcia1986/dc473b80dcecbee3458e to your computer and use it in GitHub Desktop.
verificar se um PID está ativo (unix e windows 32bits)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import platform | |
import ctypes | |
def pid_ativo(pid): | |
if platform.system() == "Windows": | |
return _pid_ativo_windows(pid) | |
else: | |
return _pid_ativo_unix(pid) | |
def _pid_ativo_unix(pid): | |
try: | |
os.kill(pid, 0) | |
except OSError: | |
return False | |
return True | |
def _pid_ativo_windows(pid): | |
import ctypes.wintypes | |
_AINDA_ATIVO = 259 | |
kernel32 = ctypes.windll.kernel32 | |
handle = kernel32.OpenProcess(1, 0, pid) | |
if handle == 0: | |
return False | |
exit_code = ctypes.wintypes.DWORD() | |
ativo = (kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)) == 0) | |
kernel32.CloseHandle(handle) | |
return ativo or exit_code.value == _AINDA_ATIVO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment