Setting up python .py to run in background and on startup — by Steven Lacerda (Morgan Hill, CA)
I have had a heck of time figuring out how to run my python script in a windows environment. I tried pyinstaller, py2exe, nssm, all to no avail. Every single one threw out errors of some sort, so I fell back to the following:
- Run the terminal created by my script in the background, so users cannot accidentally kill my script.
- Created a task in Task Scheduler to start my script at startup.
First, to have my script run in the background, I had to create a file called whatever I wanted .vbs.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\Administrator\Downloads\VSOM\VSOM\flask\start-flask.bat" & Chr(34), 0
Set WshShell = Nothing
Your filename in the above is your .bat file. My start-flask.bat file does nothing more than start the flask server:
flask run -h 0.0.0.0 -p 5000
Now, if you double click on the .vbs file you created, in my case flask-app.vbs, my script starts, but no terminal window…WOOOHOOOO!!!
Now, open Task Scheduler and create a task with a trigger on startup to start your script.
I won’t go into Task Scheduler because it is pretty self-explanatory.
Solving a problem is as good as those sexual feelings…sometimes, okay, maybe not. Straight from Morgan Hill, CA. Good luck!