使用python杀死特定用户帐户下的进程

编程入门 行业动态 更新时间:2024-10-28 18:36:26
本文介绍了使用python杀死特定用户帐户下的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想杀死一个应用程序(树),例如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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 20:01:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1123465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帐户   进程   用户   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!