0

When you go to Task Manager and select an app and press "End Task", the program closes. How can I do this process in python? So when the code runs, for example, the Chrome app closes.

not_speshal
  • 22,093
  • 2
  • 15
  • 30
Parsa Ad
  • 91
  • 6
  • Start by reviewing the *psutil* module. From there you will be able to identify the process you want to kill. Then study the *os* module which supports a kill function – DarkKnight Mar 02 '22 at 15:56
  • `os.system(f'taskkill /F /PID {pid_number}')` where pid_number is a variable containing the pid of the process you want to kill –  Mar 02 '22 at 15:57
  • hey sembei , good work , it worked. but some of the programs do not have a PID number. like chrome and etc. – Parsa Ad Mar 02 '22 at 16:12

2 Answers2

0

Clone of No.6278847: Is it possible to kill a process on Windows from within Python?

You can do that through the following code:

import os

def terminate(ProcessName):
    os.system('taskkill /im ' + ProcessName)

terminate('chrome.exe')

Credits: Nix

Dharman
  • 30,962
  • 25
  • 85
  • 135
Danny
  • 77
  • 1
  • 7
0

Couple of links related to this: (1) kill process using os package (2) Kill process using wmi package in windows