Friday, December 27, 2024
Google search engine
HomeLanguagesHow to Terminate a running process on Windows in Python?

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

Precautions

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.

Method 1:

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

Code:

Python3




# import wmi library
import wmi
 
# This variable ti would be used
# as a parity and counter for the
# terminating processes
ti = 0
 
# This variable stores the name
# of the process we are terminating
# The extension should also be
# included in the name
name = 'Process_Name'
 
# Initializing the wmi object
f = wmi.WMI()
  
# Iterating through all the
# running processes
for process in f.Win32_Process():
     
    # Checking whether the process
    # name matches our specified name
    if process.name == name:
 
        # If the name matches,
        # terminate the process   
        process.Terminate()
     
        # This increment would acknowledge
        # about the termination of the
        # Processes, and would serve as
        # a counter of the number of processes
        # terminated under the same name
        ti += 1
 
 
# True only if the value of
# ti didn't get incremented
# Therefore implying the
# process under the given
# name is not found
if ti == 0:
 
    # An output to inform the
    # user about the error
    print("Process not found!!!")


Explanation: 

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. 

Method 2:

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.
Code:

Python3




# import os module
import os
 
# delete given process
os.system('wmic process where name="Process_Name" delete')


Output:

Deleting instance \\DESKTOP-LI99O93\ROOT\CIMV2:Win32_Process.Handle=”5140″ 
Instance deletion successful. 
 

RELATED ARTICLES

Most Popular

Recent Comments