views:

2547

answers:

4

Is there a way to terminate a process started with the subprocess.Popen class with the "shell" argument set to "True"? In the working minimal example below (uses wxPython) you can open and terminate a Notepad process happily, however if you change the Popen "shell" argument to "True" then the Notepad process doesn't terminate.

import wx
import threading
import subprocess

class MainWindow(wx.Frame):

    def __init__(self, parent, id, title):        
        wx.Frame.__init__(self, parent, id, title)
        self.main_panel = wx.Panel(self, -1)

        self.border_sizer = wx.BoxSizer()

        self.process_button = wx.Button(self.main_panel, -1, "Start process", (50, 50))
        self.process_button.Bind(wx.EVT_BUTTON, self.processButtonClick)

        self.border_sizer.Add(self.process_button)
        self.main_panel.SetSizerAndFit(self.border_sizer)
        self.Fit()

        self.Centre()
        self.Show(True)

    def processButtonClick(self, event):
        if self.process_button.GetLabel() == "Start process":
            self.process_button.SetLabel("End process")
            self.notepad = threading.Thread(target = self.runProcess)
            self.notepad.start()
        else:
            self.cancel = 1
            self.process_button.SetLabel("Start process")

    def runProcess(self):
        self.cancel = 0

        notepad_process = subprocess.Popen("notepad", shell = False)

        while notepad_process.poll() == None: # While process has not yet terminated.
            if self.cancel:
                notepad_process.terminate()
                break

def main():
    app = wx.PySimpleApp()
    mainView = MainWindow(None, wx.ID_ANY, "test")
    app.MainLoop()

if __name__ == "__main__":
    main()

Please accept for the sake of this question that "shell" does have to equal "True".

+3  A: 

When using shell=True and calling terminate on the process, you are actually killing the shell, not the notepad process. The shell would be whatever is specified in the COMSPEC environment variable.

The only way I can think of killing this notepad process would be to use Win32process.EnumProcesses() to search for the process, then kill it using win32api.TerminateProcess. You will however not be able to distinguish the notepad process from other processes with the same name.

Thomas Watnedal
A: 

Python 2.6 has a kill method for subprocess.Popen objects.

http://docs.python.org/library/subprocess.html#subprocess.Popen.kill

S.Lott
The documentation says that kill is an alias for terminate on the Windows platform. Thats what he is using in the example that does not work.
Thomas Watnedal
-1: kill method will kill the shell, not a solution
nosklo
@nosklo: you've tested this? I can't test it; I don't have 2.6 installed on Windows.
S.Lott
+3  A: 

Why are you using shell=True?

Just don't do it. You don't need it, it invokes the shell and that is useless.

I don't accept it has to be True, because it doesn't. Using shell=True only brings you problems and no benefit. Just avoid it at all costs. Unless you're running some shell internal command, you don't need it, ever.

nosklo
+1  A: 

Based on the tip given in Thomas Watnedal's answer, where he points out that just the shell is actually being killed in the example, I have arranged the following function which solves the problem for my scenario, based on the example given in Mark Hammond's PyWin32 library:

procname is the name of the process as seen in Task Manager without the extension, e.g. FFMPEG.EXE would be killProcName("FFMPEG"). Note that the function is reasonably slow as it performs enumeration of all current running processes so the result is not instant.

import win32api
import win32pdhutil
import win32con

def killProcName(procname):
    """Kill a running process by name.  Kills first process with the given name."""
    try:
        win32pdhutil.GetPerformanceAttributes("Process", "ID Process", procname)
    except:
        pass

    pids = win32pdhutil.FindPerformanceAttributesByName(procname)

    # If _my_ pid in there, remove it!
    try:
        pids.remove(win32api.GetCurrentProcessId())
    except ValueError:
        pass

    handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pids[0])
    win32api.TerminateProcess(handle, 0)
    win32api.CloseHandle(handle)
Wayne Koorts
-1: solutuion kills other processes that happen to be running with the same name.
nosklo
As I said, the solution works for MY particular scenario in which case there will always only be one running instance of the process I need to kill.
Wayne Koorts
Also the comment in the code which said that every instance is killed was incorrect, and I have updated it.
Wayne Koorts