Search code examples
pythonwindowssubprocessmonitor

Python script waiting for some program being launched and then starting another program. (Windows)


I would like to write a python script that will finally be converted to .exe (with pyinstaller lets say) and added to windows startup applications list. This program (once launched) should 'monitor' user and wait until user will start a specified program (say program1.exe) and then my python program should launch another specified program (program2.exe).

I know that there is something like subprocess to launch another program from python script but I was not able to make it work so far ;/ And as it comes to this part where I need to monitor if user does start specified program I have no idea haw to bite on this. I don't expect a complete solution (although it would be very nice to find such ;p) but any guides or clues would be very helpfull.


Solution

  • For the monitoring whether or not the user has launched the program, I would use psutil: https://pypi.python.org/pypi/psutil and for launching another program from a python script, I would use subprocess.

    To launch something with subprocess you can do something like this:

    PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
    subprocess.call([PATH_TO_MY_EXTERNAL_PROGRAM])
    

    If it is as simple as calling an exe though, you could just use:

    PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
    os.system(PATH_TO_MY_EXTERNAL_PROGRAM)
    

    Hope this helps.

    -Alex