55

I'm using Python 2.6. Sometimes there become several instances of a certain process open, and that process causes some problems in itself. I want to be able to programatically detect that there are multiple instances of that process and to kill them.

For example, maybe in some cases there are 50 instances of make.exe open. I want to be able to tell that there are 20 instances open, and to kill them all. How is this accomplished?

cupof
  • 613
  • 1
  • 5
  • 6
  • The answers below will work, but you could try using `psutil`: http://psutil.readthedocs.io/en/latest/#kill-process-tree – szmoore Apr 24 '18 at 05:24

6 Answers6

70

I would think you could just use taskkill and the Python os.system()

import os
os.system("taskkill /im make.exe")

Note: I would just note you might have to fully qualify the taskkill path. I am using a Linux box so I can't test...

djvg
  • 11,722
  • 5
  • 72
  • 103
Nix
  • 57,072
  • 29
  • 149
  • 198
  • 7
    And you can use `os.popen('tasklist').readlines()` to get a list of the processes currently executing, and thus can count the number of instances the process you're looking for appears in the list and use that to determine if it's time to kill all of them. Usage of `subprocess.Popen()` is a bit more complicated, but `os.popen()` was deprecated starting with Python 2.6, so there's that to keep in mind. See http://stackoverflow.com/questions/3215262/monitor-process-in-python/3215404#3215404 for how to utilize `subprocess.Popen()` for this same sort of task. – JAB Jun 08 '11 at 19:04
  • Note that `os.popen('tasklist').read()` could be used to get the entire list as one string, if there's a `str` function to count the occurrences of a substring within a larger string. – JAB Jun 08 '11 at 19:13
  • 1
    `subprocess.check_output('tasklist')` will do much better at error handling – Eric Sep 06 '17 at 23:12
  • On windows 10 it needs a `/F` flag to force terminate. – minghua Sep 07 '19 at 00:16
  • os.popen is still live and kicking in 3.9, and don't give any deprecation warnings, fyi – Berry Tsakala Nov 04 '20 at 10:43
  • Is there any way to suppress the output? – Shub Jul 31 '21 at 16:32
32

Yes,You can do it

import os
os.system("taskkill /f /im  Your_Process_Name.exe")
  1. /f : Specifies that process(es) be forcefully terminated.
  2. /im (ImageName ): Specifies the image name of the process to be terminated.
  3. For more info regarding TaskKill
Avinash Jeeva
  • 512
  • 5
  • 10
10

There is a nice cross-platform python utility psutil that exposes a kill() routine on a processes that can be listed with psutil.process_iter().

There is already an example in the other thread: https://stackoverflow.com/a/4230226/4571444

Pshemy108
  • 313
  • 3
  • 10
4

I think the code is like this will work:

import os

def terminate(ProcessName):
    os.system('taskkill /IM "' + ProcessName + '" /F')

terminate('chrome.exe')
Mkrita
  • 41
  • 5
3

How about this, I tested it with ActiveState Python 2.7:

import sys, traceback, os

def pkill (process_name):
    try:
        killed = os.system('tskill ' + process_name)
    except Exception, e:
        killed = 0
    return killed

call it with:

pkill("program_name")
Giovanni
  • 31
  • 1
3

You can use the TerminateProcess of the win32 api to kill a process. See the following example : http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/

You need to give it a process handle. If the process is started from your code, the process handle is returned by the CreateProcess or popen.

If the process was started by something else, you need to get this handle you can use EnumProcess or WMI to retrieve it.

luc
  • 41,928
  • 25
  • 127
  • 172