0

I have tried both terminate() and kill() but both have failed to stop a subprocess I start in my python code.

Is there any other way?

On Windows with Python 2.7

I have also tried the following with no results...

os.kill(p.pid, signal.SIGTERM)

and

import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, theprocess.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
Takkun
  • 8,119
  • 13
  • 38
  • 41

2 Answers2

0

You could use the os.system('taskkill') here:http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true

P'sao
  • 2,946
  • 11
  • 39
  • 48
0

Try using psutil, or another way to remotely kill the process itself (psutil is cross platform so the code is nicer):

p = psutil.Process(pid)
p.terminate()  #or p.kill()

Code taken from How to terminate process from Python using pid?.

Note that if using shell=True, the PID is of the shell and not any process it spawned. To kill a subprocess with shell=True you may look at How to terminate a python subprocess launched with shell=True

Rotem Shalev
  • 138
  • 1
  • 13