Task Scheduler for GIS Tasks - Skipping Python Scripts Called from Bat File

329
14
3 weeks ago
MichaelVolz
Esteemed Contributor

I have been running scheduled tasks using Windows Task Scheduler for many years with the ArcMap version of python.  Now that my org is migrating to ArcGIS Pro, I am finding that the same bat file is now randomly skipping the ArcGIS Pro python scripts.  The bat file runs fine outside of Windows Task Scheduler, so both the python scripts and bat file itself are fine.  Sometimes even the first python script in the bat file gets skipped with Windows Task Scheduler, so associating each python script with a bat file will not guarantee success with that setup.

Has anyone encountered an issue like this with a similar setup where you call multiple python scripts sequentially so once one python script has finished running the next python script is then executed?

Does anyone use a different job schedule software other than Windows Task Scheduler to run GIS jobs?

Any help or feedback is greatly appreciated.

 

0 Kudos
14 Replies
jcarlson
MVP Esteemed Contributor

What does the bat file look like? I never used ArcMap, but with Pro, you need to activate the conda environment first, then call the python file, so your bat file should include another line for that.

Esri's docs suggest using a built-in propy.bat script rather than calling python directly.

https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/using-conda-with-arcgis-pro.htm

- Josh Carlson
Kendall County GIS
0 Kudos
MichaelVolz
Esteemed Contributor

I have already changed the bat file to use propy.bat, but the python scripts still get randomly skipped.

0 Kudos
jcarlson
MVP Esteemed Contributor

Curious! So, are you certain they're being skipped, as opposed to erroring out? And the script(s) being skipped aren't consistent? I have had scripts that worked fine as a bat file and separate python files, but would fail in the Task Scheduler. Sometimes the Task Scheduler showed an error, sometimes it claimed the script exited successfully. It wasn't always clear why, unfortunately.

The data involved in our scripts changed daily, and sometimes it was an unexpected datatype conflict between source and destination databases, or the ArcGIS Python API and Pandas implementing datetime values in conflicting ways. May not be the case for your scripts, though.

Do you have any logging going on? I would try starting each script with something like

import logging

logging.basicConfig(
    filename = 'some-log-file.log',
    level = logging.DEBUG
)

logging.debug('Starting some script')

That would at least confirm if the scripts are starting at all.

To answer your other question about alternatives: I got really tired of issues like these, and reworked my scripts so that they would run on a lightweight Linux machine, then added the scripts to the machine's crontab to run at set times.

I don't know what your scripts look like and how feasible such a thing would be, but it was absolutely worth the effort. Haven't had a problem since.

- Josh Carlson
Kendall County GIS
0 Kudos
MichaelVolz
Esteemed Contributor

Josh:

Are you sure you need to need to activate the conda environment if you are just using python modules that are packaged with ArcGIS Pro by default?

What modifications would need to be done to the script to run on a Linux machine?  Did you need to install ArcGIS Pro onto the Linux machine?

0 Kudos
jcarlson
MVP Esteemed Contributor

For me, absolutely, but that's because I'm using python for lots of other things, and I've got half a dozen different python envs. If I don't specify which, it just uses the base env, which won't have the modules I need.

It's worth nothing that I primarily use the ArcGIS Python API, not ArcPy, so it's a little easier to use in Linux. Been a while since I tried ArcPy in Linux, but you can download arcpy through the Esri conda channel. Authentication is different w/o Pro installed.

EarthData has a neat approach using Wine: https://www.earthdata.nasa.gov/learn/gis/gis-how-tos/run-arcpy-on-arcgis-linux

- Josh Carlson
Kendall County GIS
0 Kudos
MichaelVolz
Esteemed Contributor

Josh:

At what interval(s) do you need your various GIS scheduled tasks to run at?

I ask because tasks that need to get run once a day seem to be fine, but when a task needs to be run multiple times per day (e.g. hourly) that is when the skipping of python scripts occurs.

0 Kudos
jcarlson
MVP Esteemed Contributor

Our scripts run daily at most. Is it possible that a prior run is hung up on something, preventing later runs from kicking off properly? I don't know for sure, but I could imagine that a script utilizing ArcPy might have some constraints with checking out a license?

Did you try adding a log statement at the very beginning of the script?

- Josh Carlson
Kendall County GIS
0 Kudos
MichaelVolz
Esteemed Contributor

Hi Josh:

When we put a print statement in before import arcpy we get that to be logged, but then the script just exits.

When you clone your python environments, do you use ESRI's interface or do you perform this using Conda command line (A GIS consultant my org works with says that they have encountered issues with ESRI's interface for cloning and now only use Conda command line)?

0 Kudos
jcarlson
MVP Esteemed Contributor

So the script isn't skipping, but something about importing arcpy throws it off… For debugging purposes, you could try wrapping sections of your code in try / except blocks, maybe? I'm not quite sure how to further diagnose why importing arcpy crashes it out.

Similar to your consultant, we use the conda CLI directly for managing our envs.

- Josh Carlson
Kendall County GIS
0 Kudos