Scheduling a ModelBuilder model using Task Scheduler

There are several workflows for running a model as a scheduled task. The workflow that many consider by default is to export the model as a Python script and run this Python script as a scheduled task in task scheduler. There is a better way than this, though it still requires some knowledge of Python.

If you save your model within a toolbox, it is possible to build a script that will import your model’s toolbox then run the model. The benefit of this workflow is that it can use the environmental settings of the model, and won’t require any modification to run. It is a two-stage process, starting firstly with the Import of the toolbox, followed by a line of code to call the model.

The ImportToolbox aspect is probably the trickiest part of the whole workflow, as you first have to understand aliasing. An alias is an alternative name for the toolbox, most likely in the form of a single word that describes the toolbox to you. The ImportToolbox function depends on an alias being set. This could be done when the Toolbox was initially created, or on the fly if one hasn’t been set previously.

Syntax                                          

For technical details see the Help documentation.  

The ImportToolbox function has two possible inputs. The first is the inputFile path to the toolbox .tbx file. If you don't have a toolbox alias set you will have to define the second optional input, moduleName. After importing the toolbox, you have to call the model. The model is called on the fly through the arcpy module and has a particular syntax that must be used to call it successfully.

It is very important to avoid underscores when setting the toolbox alias as an underscore indicates the end of the alias string. You should also use camel case (e.g. toolboxAlias) for readability. 

Code Samples

Using the following code samples, you should be able to determine how to import your toolbox and instantiate your model/tool. For sample purposes, the toolbox will be called “Toolbox”, the toolbox alias will be "toolboxAlias", the model name will be “modelName” and the model will have a single parameterised input feature class called "modelInput". 

Note, the model alias (if you have one, not to be confused with the toolbox alias) is not used at all in this process. 

If Toolbox has a predefined alias, then run the model:

import arcpy

arcpy.ImportToolbox("C:/Toolbox.tbx")

arcpy.modelName_toolboxAlias("C:/Test.gdb/modelInput")

Else set Toolbox alias on the fly when importing, then run the model:

import arcpy

arcpy.ImportToolbox("C:/Toolbox.tbx","toolboxAliasOnTheFly")

arcpy.modelName_toolboxAliasOnTheFly("C:/Test.gdb/modelInput")

Once your Toolbox has been imported, the model can be called in one of two ways:

arcpy.toolboxAlias.modelName("C:/Test.gdb/modelInput")

or;

arcpy.modelName_toolboxAlias("C:/Test.gdb/modelInput")

But if the model has no parameterised input or output, the function would be called as follows:

arcpy.toolboxAlias.modelName()

or;

arcpy.modelName_toolboxAlias()

Task Scheduler

Once you have successfully built up your Python script to call your model, the next step would be to automate this script in Windows Task Scheduler. When creating a task there are a couple of key parameters to look out for:

Ensure the task is "Run whether the user is logged on or not" and "Run with the highest privileges"

In the task action run the Python script from the folder where it is stored. To do this complete the “Start In” parameter with the folder location (in this example – C:\NightlyUpdate) of your Python script.

Finally ensure that you set up scripts on a Server/PC that will always be powered on at the time the task is due to run.