使用 PSUTIL 杀死进程 [英] Killing processes with PSUTIL

查看:68
本文介绍了使用 PSUTIL 杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望编写一些代码,根据进程的名称和拥有者来终止进程.这在 Windows XP 上运行良好,但是当我在 Windows 7 上运行相同的代码时,我在尝试获取进程的用户名时收到拒绝访问错误.

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.

是否有更简单的方法可以杀死可以在 XP 和 Win7 上运行的进程?

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

实际上需要检查进程是否归SYSTEM"所有,这样我就可以检查进程何时完成用户进程,因为 SYSTEM 进程仍然存在,我不关心这个.

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()

推荐答案

如果您没有使用 PSUTIL 终止进程的权限,那么您将无法使用 其他任何东西.显然,首先想到的是 UAC,它恰好出现在 XP 和 Windows 7 之间. 这意味着您的 PSUTIL 必须从提升的提示符下运行,这并不奇怪.将清单添加到请求提升.

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.

这篇关于使用 PSUTIL 杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆