Here's a more complete DOS batch file that uses a loop to check return codes from the python.exe. It will repeat the loop until it gets a return code == 0 or until the maximum number of tries is reached. So far, I haven't seen it go over 7 tries. Note: This is only checking the problem where Windows Task Scheduler is not executing python.exe. It is not checking to see if there was an error within the python script.
--------------------------------
SET log_file=c:\temp\Log.txt
ECHO ######################################################################### >> %log_file%
ECHO Batch start %date% Time: %time% >> %log_file%
SET /A _counter=1
SET /A _max_tries=21
SET /A _wait_seconds=2
:LOOP
IF %_counter%==%_max_tries% (
ECHO GOTO END - max tries of [%_max_tries%] reached. >> %log_file%
GOTO :END
)
ECHO Attempt: [%_counter%] >> %log_file%
ECHO Error level before python: [%errorlevel%] >> %log_file%
:: Use the python exe to run our batch script.
:: Use full path to both python exe and python script.
"c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe" c:\temp\process.py
ECHO Error level after python: [%errorlevel%] >> %log_file%
:: Check the python return code. If success then exit Else pause and try again.
IF %errorlevel%==0 (
ECHO GOTO END - success >> %log_file%
GOTO :END
)
ELSE (
ECHO Wait for some time. >> %log_file%
TIMEOUT %_wait_seconds% > NUL
)
ECHO After pause %date% Time: %time% >> %log_file%
:: Increment the counter.
SET /A _counter=%_counter%+1
GOTO :LOOP
:END
ECHO Batch end %date% Time: %time% >> %log_file%