Skip to content Skip to sidebar Skip to footer

Kill Process Under Specific User Account With Python

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

Solution 1:

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

Solution 2:

os.system('taskkill /f /t /im MyApp.exe /fi "username eq my_user"')

shout out to Mathias R. Jessen

Post a Comment for "Kill Process Under Specific User Account With Python"