I have the following problem
JavaScript
x
29
29
1
import os
2
import json
3
import wmi
4
from random import choice
5
import time
6
7
filename = "kill.json"
8
with open(filename) as file:
9
kill = json.load(file)
10
11
def taskKill(imageNames: list):
12
cmdPrefix = 'taskkill /F /IM '
13
for imageName in imageNames:
14
cmd = cmdPrefix + imageName
15
os.system(cmd)
16
17
18
while 1==1:
19
c=wmi.WMI()
20
def check_process_running(rty):
21
if(c.Win32_Process(name=rty)):
22
print("Process is running")
23
taskKill(kill)
24
return
25
else:
26
print("Process is not running")
27
StrA =choice(kill)
28
check_process_running(StrA)
29
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:
JavaScript
1
6
1
import os, wmi
2
c = wmi.WMI()
3
for process in c.Win32_Process(name="python.exe"):
4
print(process.ProcessId, process.Name)
5
print("current processId:", os.getpid())
6