I have the following problem
import os import json import wmi from random import choice import time filename = "kill.json" with open(filename) as file: kill = json.load(file) def taskKill(imageNames: list): cmdPrefix = 'taskkill /F /IM ' for imageName in imageNames: cmd = cmdPrefix + imageName os.system(cmd) while 1==1: c=wmi.WMI() def check_process_running(rty): if(c.Win32_Process(name=rty)): print("Process is running") taskKill(kill) return else: print("Process is not running") StrA =choice(kill) check_process_running(StrA)
In this code that detects if the program is open and closes it, no matter how I change it, it always says Process is not running.
Advertisement
Answer
The output of your script is depending on the line if(c.Win32_Process(name=rty))
– it seems the return of Win32_Process is always True
.
- Insert a print statement with the value of
Win32_Process
before this line - Have you tried to provide the argument as a String (“StrA” instead of StrA)?
To check all current running processes, use:
import os, wmi c = wmi.WMI() for process in c.Win32_Process(name="python.exe"): print(process.ProcessId, process.Name) print("current processId:", os.getpid())