Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to kill a process on Windows from within Python?

I'm using Python 2.6. Sometimes there become several instances of a certain process open, and that process causes some problems in itself. I want to be able to programatically detect that there are multiple instances of that process and to kill them.

For example, maybe in some cases there are 50 instances of make.exe open. I want to be able to tell that there are 20 instances open, and to kill them all. How is this accomplished?

like image 424
cupof Avatar asked Jun 08 '11 12:06

cupof


People also ask

How do you kill a specific process in Python?

A process can be killed by calling the Process. kill() function. The call will only terminate the target process, not child processes. The method is called on the multiprocessing.

How do I close an EXE file in Python?

If you're using Popen , you should be able to terminate the app using either send_signal(SIGTERM) or terminate() .


2 Answers

I would think you could just use taskkill and the Python os.system()

import os os.system("taskkill /im make.exe") 

Note: I would just note you might have to fully qualify the taskkill path. I am using a Linux box so I can't test...

like image 150
Nix Avatar answered Sep 22 '22 11:09

Nix


Yes,You can do it

import os os.system("taskkill /f /im  Your_Process_Name.exe") 
  1. /f : Specifies that process(es) be forcefully terminated.
  2. /im (ImageName ): Specifies the image name of the process to be terminated.
  3. For more info regarding TaskKill
like image 43
Avinash Jeeva Avatar answered Sep 23 '22 11:09

Avinash Jeeva