Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing processes with psutil

I'm looking to write some code that will kill off a process based on it's name and who owns it. This works fine on Windows XP but when I come to run the same code on Windows 7 I get Access Denied errors when trying to get the username of the process.

Is there an easier way to kill a process that will work on XP and Win7?

The check to see if the process is owned by the 'SYSTEM' is actually needed so I can check when the process has user processes are finished, as the SYSTEM process remains, and I'm not concerned with this one.

PROCNAME = 'python.exe'
for proc in psutil.process_iter():
  if proc.name == PROCNAME:
    p = psutil.Process(proc.pid)
  
    if not 'SYSTEM' in p.username:
      proc.kill()
like image 838
user1351549 Avatar asked Aug 21 '12 09:08

user1351549


1 Answers

If you don't have the privilege to kill a process with PSUTIL, you're not going to have it with anything else. The first thing that comes to mind is, obviously, UAC, which appeared exactly between XP and Windows 7. Which implies that your PSUTIL must run from an elevated prompt, not surprising. Add a manifest to request elevation.

like image 65
Remus Rusanu Avatar answered Sep 23 '22 21:09

Remus Rusanu