
How to close Windows process with python
When automating some tasks in Windows OS, you may wonder how to automatically close Windows process if you do not have the direct control of the running application or when the application is just running for too long time. In this article, I will be sharing with you how to close the Windows process with some python library, to be more specific, the pywin32 library.
Prerequisites
You will need to install the pywin32 library if you have not yet installed:
pip install pywin32
Find the process name from Windows Task Manager
You will need to first find out the application name which you intend to close, the application name can be found from the Windows task manager. E.g. If you expand the “Windows Command Processor” process, you can see the running process is “cmd.exe”.
Let’s get started with the code!
Import the below modules that we will be using later:
from win32com.client import GetObject from datetime import datetime import os
And we need to get the WMI (Windows Management Instrumentation) service via the below code, where we can further access the window processes. For more information about WMI, please check this.
WMI = GetObject('winmgmts:')
Next, we will use the WMI SQL query to get the processes from the Win32_Process table by passing in the application name. Remember we have already found the application name earlier from the task manager.
for p in WMI.ExecQuery('select * from Win32_Process where Name="cmd.exe"'): #the date format is something like this 20200613144903.166769+480 create_dt, *_ = p.CreationDate.split('.') diff = datetime.now() - datetime.strptime(create_dt,'%Y%m%d%H%M%S')
There are other properties such as Description, Status, Executable Path, etc. You can check the full list of the process properties from this win32-process documentation. Here we want to base on the creation date to calculate how much time the application has been running to determine if we want to kill it.
Assuming we need to close windows process after it is running for 5 minutes.
if diff.seconds/60 > 5: print("Terminating PID:", p.ProcessId) os.system("taskkill /pid "+str(p.ProcessId))
With this taskkill command, we will be able to terminate all the threads under this Windows process peacefully.
Conclusion
The pywin32 is super powerful python library especially when dealing with the Windows applications. You can use it to read & save attachments from outlook, send emails via outlook , open excel files and some more. Do have a check on these articles.
As per always, welcome any comments or questions.
Hello,
I added this tutorial to PyCharm. The code works just fine, but your if diff.seconds/60 > 5: scenario does not work right.
This will kill off CMD.exe as soon as I run the program after I open it.
I even changed the 5 to 300 since 300 sec is 5 mins and it still terminates the program right away.
Hi Charles,
The diff.seconds/60 should be correct to convert the seconds to minutes. If you are running this program from a bat file, you may add a delay, so that you can see the cmd window you intended to kill will be closed before your current execution window. e.g.:
In your run.bat file, you put below:
python test_close_window.py
timeout 15
It should close the other cmd windows first, and then you will see a count down before your current window closed. Hope this clarifies your problem.
Thank you kind stranger
🙂 Thank you