I have a main.py which open multiple cmd (subprocess test.py) in loop and will kill the process using taskkill if we have opened more than 2 subprocess from main.py
# test.py
import time
print("test.py started...")
time.sleep(1000) # this is so long because it is behaving like test.py is hanged
print("test.py is finished...") # before this line i want to close this terminal
#main.py
import datetime
import shelx
import subprocess
cmd2 = "python test.py"
for i in range(5):
b = subprocess.Popen(["start", "/wait", "cmd.exe", "/k", cmd2], shell=True)
pid_lst.append(b.pid)
time.sleep(1)
while len(pid_lst) > 2:
# pop first element from list
x = pid_lst.pop(0)
# cmd_2 = f"WMIC PROCESS WHERE \"ProcessID={str(x)}\" CALL TERMINATE"
cmd_2 = f"taskkill /PID {str(x)} /F"
args = shlex.split(cmd_2)
try:
y = subprocess.Popen(args, shell=False)
print("killed ", x)
except Exception as e:
print(e.args)
Main problem i am facing is: even after successfully executing taskkill command, I still have 5 cmd are opened. Is there any way where we can completely kill/terminate the cmd while it is running?