pythonpython-3.xwindowspsutil

Detecting application start up with python


I've working on a windows python program and I need it to run once I open an app. Is it possible ? If so how would I implement it ?


Solution

  • We need some information, what did you want to do? Did you wanna know, if a process is started and then you will continue you python script? Than you can do this:

    import psutil
    
    def is_process_running(processName):
        for process in psutil.process_iter(): # iterates through all processes from your OS
            try: 
                if processName.lower() in process.name().lower(): # lowers the name, because "programm" != proGramm"
                    return True # if the process is found, then return true
            except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): # some failures what could go wrong
                pass
        return False;
    
    while(!is_process_running('nameOfTheProcess') # only continue as long as the return is False
        time.sleep(10) # wait 10 seconds
    

    For further information: psutil-docs