-1

So i have a script with the code

import os
import subprocess
import psutil

def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
#Iterate over the all the running process
for proc in psutil.process_iter():
    try:
        # Check if process name contains the given name string.
        if processName.lower() in proc.name().lower():
            return True
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
        pass
return False;

Then after executes

while True:
if checkIfProcessRunning('TDR'):
    print('TDR (tdr.exe) is running')
else:
    print('TDR (tdr.exe) is not running')
    subprocess.call("cmd /c data.vbs") # Executes other code

This whole script detects if the process tdr.exe is open or not, when the code detects that it isn't open i want it to open some other code but i want it to only do it once instead of looping.

PackedUP32
  • 17
  • 4

1 Answers1

0

Help me understand, the issue here is that after calling your "other code" a.k.a data.vbs while keeps on executing it? Add a break after subprocess.call("cmd /c data.vbs"). Or structure it like this:

while True:
    if not checkIfProcessRunning('TDR'):
        break
    print('TDR (tdr.exe) is running')

# Once we find TDR is dead, we execute our other code
print('TDR (tdr.exe) is not running')
subprocess.call("cmd /c data.vbs") # Executes other code

I hope this helps :D

BoredMan
  • 86
  • 1
  • 1
    i tried your trick and replaced the code, but after running i got the error `TypeError: checkIfProcessRunning() missing 1 required positional argument: 'processName'` – PackedUP32 Apr 13 '21 at 15:38
  • It seems that you did call checkIfProcessRunning without passing any arguments. – BoredMan Apr 13 '21 at 16:02