How to Terminate a running process on Windows in Python?
Process is a program that is being executed (processed). A process may not have to be one ran explicitly by the user, it could be a system process spawned by the operating system. Any applications that execute on an operating system firstly creates a process of its own to execute. In a typical os installation, most processes are os services and background applications, that are ran to maintain the operating system, software, and hardware. In this article, we will take a look at different ways of terminating running processes on a Windows OS, through python. Firstly we would describe a python method to achieve the result and then would look at a command found in Windows Command Processor for the equivalent effect.
NOTE: This method is exclusive to Windows Operating systems. To achieve a similar effect on Linux, macOS refer to Kill command in Linux
Terminating a concurrently running process should be done appropriately and with a conscience. As terminating a necessary process (ex. svhost, System, Windows Explorer, Antimalware Service Executable) could lead to inconsistency in operating system functionality, which could consequently lead to system crashes, blue screen of death, software malfunctioning, etc. Therefore it is generally advised to double-check the name of the applications/PID that is to be terminated beforehand.
Firstly, we would be using the wmi library for getting the list of the running process, and later would use this list to search for our desired process, and if found would terminate it. In order to install the module, execute the following command in the command interpreter of your operating system:
pip install wmi
Firstly, we define a variable storing an integer value, which would serve to tell whether the process got terminated or not. This variable could also be used to determine how many processes under the same name have been terminated. Then, we specify the name of the process which we are willing to terminate. After which we initialize the WMI() class of wmi library. This allows us to use the methods found inside it such as WMI.Win32_Service, WMI.Win32_Process etc which are designed to perform different tasks. We would be using the WMI.Win32_Process function to get the list of running processes as wmi objects. Then we use the name attribute of the wmi object to the get name of the running process. After which we would be using primitive string matching to determine whether the name of the application matches the one specified before. If it does then we call the Terminate() method, to kill/terminate the process. After which we increment the value of ti, where the incremented value (or any non 0 value) would signify that at least one process has been terminated. After the end of the loop (when all the running processes names have been checked), we would check whether the value of variable ti is still 0 or not. If it is then no process got terminated, and we inform the user about the same using an Error message.
Now we would be using an inbuilt utility found inside the windows command processor named WMIC (Windows Management Instrumentation Command-Line) to terminate the running process. The command we would be using :
wmic process where name="Process_Name" delete
Where Process_Name is the name of the process that we are terminating. To implement this utility in python we would be creating an instance of Windows Command line using os.system() and would be executing the command (bypassing it as argument) over there.
Deleting instance \\DESKTOP-LI99O93\ROOT\CIMV2:Win32_Process.Handle=”5140″
Instance deletion successful.