I have been writing scripts in Python which I want to automatically run on a Windows server on a scheduled basis. I tried working with batch files very briefly, but have enthusiastically moved on to Python scripts instead and am doing fine with them. The scripts I write are easy to write and they do exactly what I want them to do when I manually execute them. I have written many different scripts now, mostly dealing with copying, deleting, and renaming files or moving directories around.
The problem is when I try to schedule Python scripts using Windows Task Scheduler, many of them fail to run (Task Scheduler says "Last Run Result = 0x1"). This happens all the time. I have had similar experiences with batch files as well (batch files which can be run manually fail to run when scheduled). Given my limited experience thus far, I would have to say this is certainly a Windows Task Scheduler problem and not a Python problem.
Here is an example Python script:
#import modules import os, shutil, datetime, subprocess #global variables zip_dir = 'Y:\7z' zip_dir_misc = 'X:\7z' zip_extension = '.7z' def newest_zip_file(directory, extension = zip_extension): return max( (os.path.join(dir_name, file_name) for dir_name, dir_names, file_names in os.walk(directory) for file_name in file_names if file_name.endswith(extension)), key=lambda fn: os.stat(fn).st_mtime) def copy_zip_file(src_dir_p, temp_dir_p): src_file = newest_zip_file(src_dir_p) new_file = temp_dir_p + '\\' + os.path.basename(src_file) shutil.copyfile(src_file, new_file) copy_zip_file(zip_dir, zip_dir_misc)
This script copies a .7z file from one server to another via a networked folder (represented as the X: partition). This script works when run manually, but not when scheduled. However, when the script is changed to copy the same .7z file to another directory on the same server (not a networked folder), the script will work flawlessly, either when executed manually or scheduled.
If I am doing something programmatically incorrect in the script above (perhaps I am referring to a networked folder improperly) then I can fix this script this one time (although I have already tried every combination I could imagine for defining the networked folder using things like the full server name). But I keep running into the same problem with totally different Python scripts which behave the same way, which brings me to the real question:
Why do seemingly valid Python scripts fail to run when initiated via the Windows Task Scheduler?
My Windows Task Scheduler configuration:
- Run whether user is logged on or not
- Run with highest privileges
- Actions (Python scripts) called directly (Start a program: abc.py), not sent as a argument to Python.exe, although I have definitely tried the Python.exe approach many times as well. I am not convinced that either approach is better than the other (as far as making the scheduled task execute), so I just simply call the script directly.
I am looking mostly for suggestions and best practices for solving this general problem, and not so much specific fixes to the example script posted above.