Python / Windows process control

S

Salim Fadhley

Does anybody know of a python module which can do process management
on Windows? The sort of thing that we might usually do with
taskmgr.exe or process explorer?

For example:

* Kill a process by ID
* Find out which process ID is locking an object in the filesystem
* Find out all the IDs of a particular .exe file
* Find all the details of a currently running process (e.g. given an
ID tell me which files it uses, niceness, runtime)

Thanks!

Sal
 
T

Tim Golden

Salim said:
Does anybody know of a python module which can do process management
on Windows? The sort of thing that we might usually do with
taskmgr.exe or process explorer?

For example:

* Kill a process by ID
* Find out which process ID is locking an object in the filesystem
* Find out all the IDs of a particular .exe file
* Find all the details of a currently running process (e.g. given an
ID tell me which files it uses, niceness, runtime)

As far as I know, the closest you're going to come here is
WMI [1]. It won't do everything you ask, though. I don't know
how to find out which processes have a lock on a filesystem
object.

When you say "Find all the ids of a particular .exe file" I
assume you mean: all the processes which were started
by running that file.

<code>
import subprocess
import time

import wmi
c = wmi.WMI ()

#
# Kill a process by id
#
notepad = subprocess.Popen (["notepad.exe"])
time.sleep (1)
for process in c.Win32_Process (ProcessId=notepad.pid):
process.Terminate ()

#
# Which process ids correspond to an .exe
#
for i in range (5):
subprocess.Popen (["notepad.exe"])

for process in c.Win32_Process (caption="notepad.exe"):
print process.ProcessId

#
# _Some_ (but not all) of the information about each file
#
for process in c.Win32_Process (caption="notepad.exe"):
print process
process.Terminate ()

</code>

HTH

TJG

[1] http://timgolden.me.uk/python/wmi.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top