使用python杀死特定用户帐户下的进程 [英] Kill process under specific user account with python

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

问题描述

我想杀死一个应用程序(树),例如MyApp.exe 在特定用户下运行.一个进程可以被杀死:

I would like to kill an application (tree), e.g. MyApp.exe which is running under a specific user. A process can be killed with:

os.system("taskkill /f /t /im  MyApp.exe")

然而,这不会杀死感兴趣的用户的应用程序!问题是可能有多个用户登录并且都在运行同一个应用程序的实例!我不希望应用程序被错误的用户杀死.

However this doesn't kill the application for the user of interest! The problem is that several users might be logged in and all running an instance of the same application! I don't want the application to be killed for the wrong user.

推荐答案

您需要管理员权限才能查看所有用户的进程.

You need admin rights to see processes of all users.

import psutil

proc_name = 'calc.exe'
bad_users = [r'desktop\user1']

for proc in psutil.process_iter():
    if proc.name().lower() == proc_name \
    and proc.username().lower() in bad_users:
        print('kill', proc.pid)
        psutil.Process(proc.pid).kill()

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

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