def kill(self): # Recipes #http://me.in-berlin.de/doc/python/faq/windows.html#how-do-i-emulate-os-kill-in-windows #http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462 """kill function for Win32""" TerminateProcess(int(self._handle), 0) # returns None
def kill_scdaemon(): killed = False try: # Works for Windows. from win32com.client import GetObject from win32api import OpenProcess, CloseHandle, TerminateProcess wmi = GetObject('winmgmts:') ps = wmi.InstancesOf('Win32_Process') for p in ps: if p.Properties_('Name').Value == 'scdaemon.exe': pid = p.Properties_('ProcessID').Value handle = OpenProcess(1, False, pid) TerminateProcess(handle, -1) CloseHandle(handle) killed = True except ImportError: # Works for Linux and OS X. pids = subprocess.check_output( "ps ax | grep scdaemon | grep -v grep | awk '{ print $1 }'", shell=True).strip() if pids: for pid in pids.split(): subprocess.call(['kill', '-9', pid]) killed = True if killed: time.sleep(0.1) return killed
def KillProcess(pid=None, processName=None, hwnd=None, signal=0, restart=False): if pid: pass elif processName: pids = GetPids(processName=processName) if pids: pid = pids[0] else: return False elif hwnd: hwnd = GetBestHwnd(hwnd) pids = GetPids(hwnd=hwnd) if pids: pid = pids[0] else: return False else: return False fullPath = GetProcessNameEx(pid=pid, fullPath=True) try: proc = OpenProcess(1, 0, pid) TerminateProcess(proc, signal) if restart: eg.plugins.System.Execute(fullPath) return True except: return False
def kill_scdaemon(): killed = False try: # Works for Windows. from win32com.client import GetObject from win32api import OpenProcess, CloseHandle, TerminateProcess wmi = GetObject("winmgmts:") ps = wmi.InstancesOf("Win32_Process") for p in ps: if p.Properties_("Name").Value == "scdaemon.exe": pid = p.Properties_("ProcessID").Value handle = OpenProcess(1, False, pid) TerminateProcess(handle, -1) CloseHandle(handle) killed = True except ImportError: # Works for Linux and OS X. return_code = subprocess.call(["/usr/bin/pkill", "-9", "scdaemon"]) # nosec if return_code == 0: killed = True if killed: sleep(0.1) return killed
def kill_process_win32(pid): """ Call to system Windows API ``TerminateProcess`` method. """ try: from win32api import TerminateProcess, OpenProcess, CloseHandle except: lg.exc() return False try: PROCESS_TERMINATE = 1 handle = OpenProcess(PROCESS_TERMINATE, False, pid) except: lg.out(2, 'bpio.kill_process_win32 can not open process %d' % pid) return False try: TerminateProcess(handle, -1) except: lg.out(2, 'bpio.kill_process_win32 can not terminate process %d' % pid) return False try: CloseHandle(handle) except: lg.exc() return False return True
def kill(self): try: PROCESS_TERMINATE = 1 handle = OpenProcess(PROCESS_TERMINATE, False, self.pid) TerminateProcess(handle, -1) CloseHandle(handle) except: pass
def _terminate_qprocess(self): try: py_proc = psutil.Process(self.process.pid()).children()[0].pid except psutil.NoSuchProcess: return children = psutil.Process(py_proc).children() if not IS_WINDOWS: os.kill(py_proc, SIGKILL) for child in children: os.kill(child.pid, SIGKILL) if IS_WINDOWS: TerminateProcess(py_proc, -1) CloseHandle(py_proc) for child in children: TerminateProcess(child.pid, -1) CloseHandle(child.pid)
def stop(self, force=True): if self.poll() is not None: return True try: PROCESS_TERMINATE = 1 handle = OpenProcess(PROCESS_TERMINATE, False, self.pid) TerminateProcess(handle, -1) CloseHandle(handle) except subprocess.pywintypes.error, e: # @TODO: check error code warnings.warn(e)
def parent(): print("parent started", os.getpid()) job = CreateJobObject(None, "my-first-job") AssignProcessToJobObject(job, GetCurrentProcess()) for i in range(3): subprocess.Popen("python main.py /child") raw_input("press any key to kill all child processes:") try: job_processes = QueryInformationJobObject(None, JobObjectBasicProcessIdList) for pid in job_processes: if pid == os.getpid(): # Don't kill ourselves continue child_handle = OpenProcess(PROCESS_TERMINATE, True, pid) TerminateProcess(child_handle, 1) print("Killed", pid) except pywintypes.error: # Only here to demonstrate what exception is raised. raise
def kill_scdaemon(): killed = False try: # Works for Windows. from win32com.client import GetObject from win32api import OpenProcess, CloseHandle, TerminateProcess wmi = GetObject('winmgmts:') ps = wmi.InstancesOf('Win32_Process') for p in ps: if p.Properties_('Name').Value == 'scdaemon.exe': pid = p.Properties_('ProcessID').Value handle = OpenProcess(1, False, pid) TerminateProcess(handle, -1) CloseHandle(handle) killed = True except ImportError: # Works for Linux and OS X. return_code = subprocess.call( # nosec ['/usr/bin/pkill', '-9', 'scdaemon']) if return_code == 0: killed = True if killed: time.sleep(0.1) return killed
def terminateProcess(process): if RUNNING_WINDOWS: TerminateProcess(process._handle, 1) else: kill(process.pid, SIGKILL)
def terminateProcess(process): TerminateProcess(process._handle, 1)
def killsubprocess(process): if process.poll() is None: TerminateProcess(int(process._handle), -1)