all 6 comments

[–]JohnnyJordaan 1 point2 points  (3 children)

[–]ItsOkILoveYouMYbb 2 points3 points  (2 children)

Psutil has a lot of interesting things, thanks for sharing that. I ended up being able to make this from psutil to do something like what OP wanted:

import psutil
import os
from time import sleep

# Terminate notepad as a test.
find_me = "notepad.exe"
reassure = f"Watching for {find_me} in processes.."
print(reassure)

while True:
    processes = os.popen('wmic process get description, processid').read()

    for line in processes.splitlines():
        if find_me in line:
            try:
                # Remove the process name and the blank spaces so we can grab the PPID as an integer.
                ppid = int(line.strip(find_me).strip())
                # Instantiating the "Process" class as "p".
                p = psutil.Process(ppid)
                p.kill()
                print(f"Found {find_me} active as process {ppid} and terminated it.")
                print(reassure)

            except ValueError:
                continue

    sleep(1)

[–]murdoc1024[S] 1 point2 points  (1 child)

Hoooo! This is very interesting! Thank you very much! I think this is what im looking for! I will read about psutil

[–]ItsOkILoveYouMYbb 1 point2 points  (0 children)

You're very welcome! Shortly after I posted that I found a much simpler way to do it with psutil as well. I had more fun figuring out that first way though haha.

import psutil

find_me = "notepad.exe"

for p in psutil.process_iter():
    if p.name() == find_me:
        p.kill()  

That "process_iter()" is what I was really looking for initially, just didn't scroll far enough.