2

I have a python script by which i am opening three executable files as follows:

Aa=subprocess.Popen([r"..\location\learning\A.exe"])
Bb=subprocess.Popen([r"..\location\learning\new\B.bat"])
Cc=subprocess.Popen([r"..\location\learning\new\B.bat"])

All the three files are getting opened. Now,next step i want kill these three opened modules.So, firstly i tried to kill "Aa" as follows:

PROCESS_TERMINATE= 1
k = ctypes.windll.kernel32klll
handle = k.OpenProcess(PROCESS_TERMINATE, False,Aa.pid)
k.TerminateProcess(handle, -1)
k.CloseHandle(handle)

But after adding these piece of lines the three modules 'Aa','Bb' and 'Cc' they don't gets opened.So,i want to know some clean solution so that firstly all the three modules gets executed and then after a while they get closed itself. As,i am using python 2.5 on windows 64 bit platform so kindly suggest solution accordingly.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
anamika email
  • 327
  • 9
  • 21
  • os.kill should be sufficient. – Darcey Mckelvey May 11 '15 at 15:14
  • have you tried to call [`taskkill` command](http://stackoverflow.com/a/10830753/4279) -- it looks like you need to kill the whole tree, not just a single subprocess? – jfs May 11 '15 at 15:30
  • @DarceyMckelvey support for os.kill in Windows was added in 2.7 – Serge Ballesta May 11 '15 at 15:32
  • Starting processes using relative paths is error prone. If the command is started with a full path from another directory you will not find your subprocess executables. – Serge Ballesta May 11 '15 at 15:37
  • You should add error handling (check `handle` is not `None`) -- I don't know whether ctypes raises exceptions automatically here. I see that `psutil-2.1.3` (that works on Python 2.5) uses the same methods to kill a subprocess on Windows as you do except it calls `TeminateProcess(handler, 0)` instead of `TeminateProcess(handler, -1)`. – jfs May 11 '15 at 16:06
  • @J.F.Sebastian i tried subprocess.call(['taskkill', '/F', '/T', '/PID', str(p.pid)]) and the termination of process "Aa" containing A.exe got successfull but the rest two modules containing "B.bat" did not get terminated succesfully. Is there some other way to kill the .bat files in python? – anamika email May 11 '15 at 17:12
  • Could add a [minimal complete code example](http://stackoverflow.com/help/mcve) for clarity? Use a dummy bat-file. What happens if you run `cmd.exe` explicitly e.g., `r'cmd.exe /c "path\to\file.bat"'`? – jfs May 11 '15 at 19:15
  • 1
    If batch file exits without waiting for the processes it starts (e.g. using the `start` command) then `cmd.exe` has already exited and `taskkill /F /T` will fail. You can try the following instead: `subprocess.call('wmic process where ParentProcessId=%d call Terminate' % p.pid)`. – Eryk Sun May 11 '15 at 20:17

1 Answers1

0

The process returned by subprocess.Popen() has a terminate() method to kill the child process since Python 2.6. To use that you would have to upgrade your Python install or install two versions of Python in parallel.

But a better way is usually to find out how you can tell this process to stop and use that. Many processes stop when you send them certain standard input or when you close the pipes which Popen created.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820