Re: howto kill a windows process by name ?



Adam Pletcher wrote:
Take a look at "killProcName.py", in the win32 extension package.

- Adam

-----Original Message-----
From: python-list-bounces+adam=volition-inc.com@xxxxxxxxxx
[mailto:python-list-bounces+adam=volition-inc.com@xxxxxxxxxx] On
Behalf
Of stef mientki
Sent: Friday, October 05, 2007 4:40 PM
To: python-list@xxxxxxxxxx
Subject: howto kill a windows process by name ?

hello,

is there a library to kill a windows process by name ?

thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list
thanks Adam,
that indeed it works,
but googling for killProcName.py I found messages about it being slow,
and indeed it's slow,
and that same google search turned up a faster method, see below.

thanks again,
Stef Mientki



import time
import win32api, win32pdhutil, win32con
import win32pdh, string


# ***********************************************************************
# ***********************************************************************
def GetAllProcesses():
object = "Process"
items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
return instances
# ***********************************************************************


# ***********************************************************************
# ***********************************************************************
def GetProcessID ( name ) :
object = "Process"
items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)

val = None
if name in instances :
hq = win32pdh.OpenQuery()
hcs = []
item = "ID Process"
path = win32pdh.MakeCounterPath( (None,object,name, None, 0, item) )
hcs.append(win32pdh.AddCounter(hq, path))
win32pdh.CollectQueryData(hq)
time.sleep(0.01)
win32pdh.CollectQueryData(hq)

for hc in hcs:
type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
win32pdh.RemoveCounter(hc)
win32pdh.CloseQuery(hq)
return val
# ***********************************************************************


"""
THIS IS SLOW !!
def Kill_Process ( process ) :
#get process id's for the given process name
pids = win32pdhutil.FindPerformanceAttributesByName ( process )
for p in pids:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p) #get process handle
win32api.TerminateProcess(handle,0) #kill by handle
win32api.CloseHandle(handle) #close api
"""

# ***********************************************************************
# ***********************************************************************
def Kill_Process_pid ( pid ) :
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
win32api.TerminateProcess(handle,0) #kill by handle
win32api.CloseHandle(handle) #close api
# ***********************************************************************


# ***********************************************************************
# ***********************************************************************
def Kill_Process ( name ) :
pid = GetProcessID ( name)
if pid:
Kill_Process_pid ( pid )
# ***********************************************************************


# ***********************************************************************
# ***********************************************************************
if __name__ == "__main__":
a = GetAllProcesses()
print a

process = 'UPD'
Kill_Process ( process )

# ***********************************************************************



.



Relevant Pages

  • Re: Function stopping a function
    ... def long_process: ... raw_input('Press to kill the bad process') ... pid = os.fork ... We all know that a function can launch the execution ...
    (comp.lang.python)
  • [EXPL] WordPress Trackback UTF-7 SQL Injection (Exploit)
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... def banner: ... def determineIsMbstringInstalled(host, pid): ... def checkUsername(host, pid, prefix, name, uid): ...
    (Securiteam)
  • Re: Periodic Fedora 9 system hangs with jumpy mouse
    ... kill -TERM pid ... to do with graphics card and driver. ...
    (Fedora)
  • Re: how can I make sure only one instance of gkrellm runs
    ... It is the standard way of probing for a process given a pid. ... "man 3p kill" says: ... since the script is executing the command ... But for a single person running a single GUI ...
    (Fedora)
  • RE: threads in perl
    ... Instead of xterm I tried to invoke some other script: ... How can I kill exec after it was started in different thread? ... But threads all share the same pid, ... When life conspires against you, and no longer floats your boat, Don't waste your time with crying, just get on your back and float. ...
    (perl.beginners)