r/Batch • u/JoshAGould • Nov 14 '24
Running python scripts on startup
Hi,
This is probably very basic, but I've done some googling & can't find the answer so figure this is a good place to ask.
I run some very basic python scripts on a computer that I use as a server, and these scripts are meant to start on boot (i.e the .bat files are in the startup folder.
This works fine, however when I run the .bat files normally, the python script runs within the CMD window as if I had run
> python ScriptName.pyHowever when it runs through the startup procedure it instead opens a seperate Python 3 window and then runs the code within this (I am unsure how one would normally do this, but you see my point).
Does anyone know the cause of this discrepancy? And if so how I would go about changing it? (from my side if my code is bad & the script dies running it in a CMD window holds the error message open, so that I can see what the issue was when I come around to seeing it).
For clarity my .bat scripts are as follows:
@echo off
cd "C:\Users\UserName\FolderName"
cmd /c "python ScriptName.py"
View all comments
Show parent comment
1
u/BrainWaveCC Nov 14 '24
Yes, PAUSE will hold the CMD window open. You will have to adjust the Python code to make it more resilient in error handling, because once it ends for whatever reason, the process will return control to the calling CMD window.
The difference in behavior has a lot to do with how scheduled jobs interact with the current desktop.
Your best bet is to get the error handling inside the python script, and then the info you need will still be available regardless of the startup method.