Schedule a Python Script using Windows Task Scheduler

23872
7
03-19-2020 06:22 AM

Schedule a Python Script using Windows Task Scheduler

Note:  ArcGIS Pro 2.5 users can easily automate scripts and models within Pro.  See the documentation here.

 

Here are the steps to execute a python script using Windows Task Scheduler.  This is very useful when you need to execute a script at a prescribed time, or when you need to continuously execute a script (i.e. every 15 minutes).  The below steps must be run from a server that has python installed.

 

1.  Go Start and search for Task Scheduler

 

2.  Under the Actions pane on the right, click Create Task

 

3.  Specify a Name for the task, Security Options, and set the Configure for to the appropriate Operating System

 

4.  Click the Triggers tab and then click New

 

5.  Specify the schedule properties.  For example, if you wanted to execute a python script every 15 minutes, set the Settings to Daily, then specify a Start date and time.  Under the Advanced settings, check Repeat task every and set this to a value.  Then set the for a duration of setting.  The below example will execute the script every 15 minutes indefinitely starting on 3/19/2020 at 9:00:00 AM.

 

6.  Click the Actions tab and click New

 

7.  Set the Action to Start a program.  The Program/Script needs to be set to the path of your python executable (python.exe). In the below example, the script will be executed using Python 3.x, so I will specify the path to the python.exe for my ArcGIS Pro install.  This is generally located at:  "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"

 

The Add argument will be the path to my script:  "C:\Projects\Scripts\NWM_UpdateFlow.py"

 

 

8.  Optionally, set any settings on the Conditions and Settings tab, then click OK to create the task.  If you chose the option to have the task execute whether the user is logged on or not, you will be presented with a dialog to enter your credentials.  The task will then be ready to execute at the Start time:

 

 

A video of the workflow can be found at the link below:

https://youtu.be/yRwuhV66av4

Comments
by Anonymous User
Not applicable

You also have the ability to export that task from Task Scheduler and import it on another server, so you don't have to go through creating it from scratch on each server.

bspivey
Occasional Contributor

If your scheduled task completes successfully (code 0x0), but nothing seems to happen as the result of your code, the cause may be how you are using relative paths in your script. Yes, you may have entered fully qualified paths to python.exe and your script (and maybe filled the "start in" item too) when configuring the scheduled task BUT the scheduled task itself starts from C:\windows\system32 and certain relative path syntax that works in an IDE (e.g., IDLE, pyCharm, etc.) will not work as a scheduled task. For example, a script containing a relative path of /pathToItem works just fine in IDLE. However, if you print out the path (to a txt file) that is created from when executed from Task Scheduler you can see that the script is actually looking for the resource in C:\windows\system32\pathToItem. Obviously, this isn't going to work. Changing the relative path to ./pathToItem is a simple fix that may allow your script to truly execute successfully from Task Scheduler. 

JesseHandler
New Contributor

Do you have any other suggestions for another reason why a scheduled task would complete successfully, but nothing seems to happen? Per your suggestion above @bspivey, I have tried both absolute file paths and relative file paths formatted as you suggested above in my python script and neither seem to make a difference. I am not getting an error message of any kind but rather it shows that the task was completed within 7 seconds. The script runs correctly outside of Task Scheduler and the task was configured exactly as shown in the original post. Thanks!

bspivey
Occasional Contributor

@JesseHandler, that's tough to answer without specifics but my suggestion, although a bit painful,  would be to start super simple and iteratively add functionality. In other words, comment out all of the code except for your import statements and add a simple function that writes 'hello world' to a file. Get that to work and then build from there. Good luck!

BlakeTerhune
MVP Regular Contributor

@JesseHandler We had a similar issue that was caused by running the script as a different user. The issue was caused by the licensing of ArcGIS Pro being saved in a user's profile and the user running the script had not opened ArcGIS Pro and set the licensing.

MichaelVolz
Esteemed Contributor

Blake - How were to able to determine "The issue was caused by the licensing of ArcGIS Pro being saved in a user's profile and the user running the script had not opened ArcGIS Pro and set the licensing."

I am interested in knowing what tools you used to determine that this was the root cause of the issue.  Which type of licensing were you useing for Pro - Named User or Concurrent Use?

BlakeTerhune
MVP Regular Contributor

@MichaelVolz

The Python scripts were scheduled to run with Windows Task scheduler. We chose the option to run the script even if the user is logged out and saved the credentials of the service account user to run as. This is how I knew what user is accessing concurrent use ArcGIS Pro licenses from our license manager on an internal server. As a troubleshooting step, I logged in with that user and attempted to launch ArcGIS Pro and immediately noticed the app was not configured with concurrent use licensing from our license server (despite having already logged in as myself on the same machine and configured concurrent use licensing for ArcGIS Pro.

I have also experienced this issue on other users' machines on which I am installing ArcGIS Pro. When I log in as myself to install and configure ArcGIS Pro, the configurations (like setting concurrent use licensing) are not there when the user logs in.

Version history
Last update:
‎03-14-2022 01:56 PM
Updated by:
Contributors