Kill process with Python on Windows

Some processes always be launched and keep running in the background and consuming system resources, you will only use occassionaly but it's not wise to simply uninstall them because you still need to use them occassionaly. You can kill them in task manager manually, or you can do it in a Python script with more flexibility. Another reason to kill processes with script is it saves a lot of time, nowadays, not only launching an application takes a lot of time, the same is true to quit an application, especially on a slow computer, sometimes click the close button will freeze my computer for a while, there are a lot aftermath to deal with to safely and cleanly shutdown such as you have unsaved modifications, but sometimes you just don't care about them considering the auto save draft and recovery is the de facto standard for mordern application, all you really want maybe just a snap, and the thing just gone, rude but hassle free.

Python is a powerful tool if you want to script in win32 API. There are many methods to interact with processes on Windows.

The psutil provides an abstract interface to process related API. It's a cross-platform library allows you retrieve the processes and system running state information such as CPU, memory, disk usage, network, etc. It's a great tool for system monitoring and administration. It implements features equivalent to Linux command such as ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap, etc. It's very easy to use

 
import psutil
 
for proc in psutil.process_iter():
    print(proc.name())
 
def kill_by_process_name(name):
    for proc in psutil.process_iter():
        if proc.name() == name:
            print("Killing process: " + name)
            if(check_process_exist_by_name(name)):
                print("Killing process: " + name + " sucess")
            else:
                print("Killing process: " + name + " failed")
            return
 
    print("Not found process: " + name)
 
def check_process_exist_by_name(name):
    for proc in psutil.process_iter():
        if proc.name() == name:
            return True
 
    return False
 
kill_by_process_name("iTunesHelper.exe")    
 

To install psutil with pip as follows, first check your platform, for example if you are using 64bit Windows and Python 3.4.3, then you should download the psutil-5.4.3-cp34-cp34m-win_amd64.whl (md5) at here, the commands:

 
>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600
Type "help", "copyright", "credits" or "license" for more information
>>> import platform
>>> platform.architecture()
('64bit', 'WindowsPE')
>>> ^Z
 
 
>pip install psutil-5.4.3-cp34-cp34m-win_amd64.whl
Processing c:\bin\psutil-5.4.3-cp34-cp34m-win_amd64.whl
Installing collected packages: psutil
Successfully installed psutil-5.4.3
 

You can also call shell command taskkill, it allows you to terminate a process forcefully

 
import os
 
def kill_by_process_name_shell(name):
    os.system("taskkill /f /im " + name)
 
kill_by_process_name_shell("iTunesHelper.exe")