killing a process in python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # linux, mac
  2. os.kill(pid, signal.SIGKILL)
  3. killedpid, stat = os.waitpid(pid, os.WNOHANG)
  4. if killedpid == 0:
  5. print >> sys.stderr, "ACK! PROCESS NOT KILLED?"
  6.  
  7. # windows
  8. handle = subprocess.Popen("someprocess here", shell=False)
  9. subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)
  10.  
  11. #also
  12. # Create a process that won't end on its own
  13. import subprocess
  14. process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])
  15.  
  16.  
  17. # Kill the process using pywin32
  18. import win32api
  19. win32api.TerminateProcess(int(process._handle), -1)
  20.  
  21.  
  22. # Kill the process using ctypes
  23. import ctypes
  24. ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)
  25.  
  26.  
  27. # Kill the proces using pywin32 and pid
  28. import win32api
  29. PROCESS_TERMINATE = 1
  30. handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
  31. win32api.TerminateProcess(handle, -1)
  32. win32api.CloseHandle(handle)
  33.  
  34.  
  35. # Kill the proces using ctypes and pid
  36. import ctypes
  37. PROCESS_TERMINATE = 1
  38. handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
  39. ctypes.windll.kernel32.TerminateProcess(handle, -1)
  40. ctypes.windll.kernel32.CloseHandle(handle)

URL: http://www.daniweb.com/software-development/python/threads/339661/1442926

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.