At the spatial analysis and geoprocessing island at this year's User Conference, several folks asked us about running a Python script or modelbuilder model at a prescribed time-usually in the early morning when their computers are bored and just waiting
Something to do. We thought it wocould be good to make a post about this and to share some tips.
The first place to start is with the help topic Scheduling
A Python script to run at prescribed times. This topic is kind of buried in the Help system so it's easy to miss. The meat of this topic shows you how to launch window 'stask
Scheduler on varous operating systems (Windows 7, XP, and so on). The screenshot below shows thetask schedask on Windows 7. There's a lot
Stuff you can do with the task schedund and you'll just have to play around with it and read the Windows Help. The actions pane, on the right,
Has the create basic task action, and this is the place to start.
Clicking on create basic task opens a wizard where you define the name of your task, the trigger (when
It runs), and the action (What program to run). The screenshot below shows the Action Tab
Where you specify the name of your Python script to run as well as any arguments to the script.
The action tab is where we have a few tips, as follows.
Run the python executable with arguments
To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of python is used in mixed environments (64bit or 32bit ), we recommend that you run the python executable
With the name of your python file as an argument to the executable.
Suppose the script you want to run isE:\My script.py
. Instead
Of running the script directly, instruct the task schedto to runpython.exe
With
The script as an argument. For example:
C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"
The locationpython.exe
Depends on your install. If you
Don't know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the locationpython.exe
As
Well as other information about your python environment.
import sysimport platformimport imp print("Python EXE : " + sys.executable)print("Architecture : " + platform.architecture()[0])print("Path to arcpy : " + imp.find_module("arcpy")[1]) raw_input("\n\nPress ENTER to quit")
After determining the locationpython.exe
, This is what
Is entered in the Action panel of the Task Scheduler:
If there are additional arguments (parameters) to your script, provide them after the path to your script. for example, the following command line executes a Python script that takes two arguments; the path to a feature class and an integer.
C:\Python27\ArcGIS10.2\python.exe "E:\My Scripts\DoConversion.py" c:\gisWork\gdb.mdb\counties 10
We typically recommend writing scripts so that they take arguments like the above example rather the hard-coding dataset paths or values within the script. however, this is one case where hard-coding paths and values in your script works
Your advantage; rather than changing argument values in the task schedes, it's easier to just edit your script to use different paths or values.
If you're familiar.bat
Files in Windows, you can have
The task scheduler run.bat
File that in turn runs your
Script with arguments. The contents of.bat
Is the single
Command Line shown above.
Remember that if any of your arguments contain spaces, You need to enclose the argument in double quotes. In the above example,E:\My
Scripts\DoConversion.py
Contains a space, so double quotes are required.
Running models at a prescribed time
A common misconception is that in order to run a model as a Python script, you must first export the model to a Python script. geoprocessing is designed so that models are tools, just like all the tools delivered with the ArcGIS. that means you can run your
Model within Python just like any other geoprocessing tool. All you need to do is import the toolbox containing your model using the importtoolbox function,
Then run your model within python.
For example, the model to the left imports.gpx
File
A Geodatabase, adds a field, calculates it to the current date and time, then appends all those features into a "master" feature class.
To run this model on a schedule, all that's needed is to execute the model within a script. Here's the Python script to run the model:
importarcpyimportosfromdatetime importdatetime # Import the toolbox containing the model. This toolbox# has an alias of "gpxtools"#arcpy.ImportToolbox(r"c:\importgpx\myGPXstuff.tbx") # Run the model. The model has two parameters, the input# .gpx file and the feature class to update#arcpy.ImportGPX_gpxtools(r"c:\ftp\inbox\datadrop.gpx", r"c:\ftp\server.sde\gpx_tracks") # Now that is has been processed, rename input gpx# to have the date and time.#os.rename(r"c:\ftp\inbox\datadrop.gpx", r"c:\ftp\inbox\datadrop{}.gpx".format(datetime.strftime(datetime.now(),"%Y%m%d"))
Advanced options
The create basic task wizard allows you to set basic properties of the task. Once the task is created, you can examine its properties and refine its triggers and actions.
Example, you can have your task run every five minutes for three hours. You can set up a task with advanced options using the Create task action.
Start simple
When I started writing this blog, it had been a long time since I messed around with scheduled tasks, so I started with a simple script that wrote information to a log file. I scheduled it to run every minute for ten minutes so I cocould test changes to the script
Soon after I made them. It was a great way to test and understand how scheduling worked before committing to running the "real" script. Here's my simple script:
importsysimportplatformimportimpprint"Importing arcpy... this may take a moment\n"importarcpy # Open a log file to write to#f=open(r'E:\schedule.log','w') # Write date/time#f.write(time.strftime('%x %X'))f.write('\n') # Test receiving of arguments/parameters via arcpy#f.write("Parameter 0 : " + arcpy.GetParameterAsText(0)+"\n") # Python info#f.write("Python EXE : " + sys.executable +"\n")f.write("Architecture : " + platform.architecture()[0]+"\n")f.write("Path to arcpy : " + imp.find_module("arcpy")[1]+"\n")
Ghislain Prince contributed to this post