Python script, when run from Task Scheduler, not returning FC list

6376
15
Jump to solution
05-31-2022 08:37 AM
KimGarbade
Occasional Contributor III

I have a pretty simply python script that steps through all of the feature classes in an enterprise GDB having a certain name.  When I run this script from the command line using a .bat file to trigger it, it runs great and completes successfully.  When I embed the same .bat file that runs the script in Windows (10) Task Scheduler, it fails to return a list of FC names.  Please see code and images below:

import arcpy
import datetime

f = open ('C:\Scripts\CartegraphIntegration\COJNPDESIntegrationDBLog.txt', 'a+')
f.write("**********Program Started :"+str(datetime.datetime.now())+"**********")
f.write('\n')
# Connection file for ArcPy
CGIntegrationsde = r"z:\Not\A\RealPath\connection.sde"
arcpy.env.workspace = CGIntegrationsde
f.write("Workspace set successfully: " + CGIntegrationsde)
f.write('\n')
#all of the feature classes we want to udpate all start with the word "Spatial"
#but since the names are fully qualified we have to account for other leading characters
featClasses = arcpy.ListFeatureClasses('*Spatial*','','')
f.write("Feature Class List acquired successfully")
f.write('\n')
if not featClasses:
    f.write("The list is empty.")
    f.write('\n')
else:
    for fc in featClasses:
    
        f.write("Updating fields to use appropriate case in FC: " + fc + " :"+str(datetime.datetime.now()))
        f.write('\n')
        
        #Script does something ArcPy like here "edited for privacy

        f.write("Updates complete in FC: " + fc + " :"+str(datetime.datetime.now()))
        f.write('\n')

f.write("**********Completed: "+str(datetime.datetime.now())+"**********")
f.write('\n')
f.close()

 When I run the script from the command line using a .bat file:

"C:\Users\?????\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone1\python.exe" "C:\Scripts\ProjectName\ScriptName.py"

I get what I want... The below image shows the log file I write in the above code:

KimGarbade_0-1654010375055.png

When I run it in the Task Scheduler I get information telling me that the list returned from the arcyp.ListFeatureClasses command is empty:

KimGarbade_1-1654010421970.png

Another weird thing is that the task always says "Running" in the scheduler even though it has reached the end of the script.

Any thoughts would be appreciated.  This is really a hassle that it works when I test it and I think I'm just an "Adding it to the scheduler" step away from being done and then that's the part the bails!

K

0 Kudos
2 Solutions

Accepted Solutions
KimGarbade
Occasional Contributor III

I cracked the case... I'm nothing if not persistent.  After writing try/except conditions and examining the description of my workspace (established using a connection file) I determined that Arcpy was indeed loading correctly and that the problem was Arcpy was not establishing a connection to my database using my connection file when my script was run from the scheduler.  So, just on a lark, I moved the "connectionfilename.sde" from its current location on a network drive to a location on the C: drive (the same folder the "scriptname.py" file was in).... and Voila it worked.  That was at least a day out of my life.  

Just for the record I tried:

  • Alternating "Run only when user is logged in" and "Run whether user is logged in or not"
  • Alternating "Run with highest privileges"
  • Running Task Scheduler as "Admin"
  • Running the python script from a batch file
  • Running "path/python.exe" as the "Program/script" with "python path/file" as the argument
  • Running "cmd" as the program and the batch file as the argument
  • Alternating the "Start in" path.... 
  • All for naught

I should have thought about the easy solution first, but your keys are always in the last place you look.

View solution in original post

BlakeTerhune
MVP Regular Contributor

Glad you got it figured out. See if it works on the network drive using a UNC path rather than a drive letter.

View solution in original post

15 Replies
BlakeTerhune
MVP Regular Contributor

Could you confirm what you have in the "Action" tab of the task? Also, is the task running as the same user you're testing with?

Here's a reference to check regarding how to set up the task in Windows Task Scheduler.
Schedule a Python script or model to run at a prescribed time: 2019 update (esri.com)

0 Kudos
KimGarbade
Occasional Contributor III

ArcGIS Pro

KimGarbade_0-1654012553906.png

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Try configuring the task action where the program/script is your Python env exe

"C:\Users\?????\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone1\python.exe"

and the optional argument is the path to your Python script file.

"C:\Scripts\ProjectName\ScriptName.py"
0 Kudos
KimGarbade
Occasional Contributor III

Sorry.  I should have mentioned that that was the first way I tried configuring it.... 

it doesn't seem to have a problem with "import arcpy" or setting the workspace.... just getting the FC list.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you connecting to the EGDB as an OS user or a database user?  If the former, what credentials is the task running as?

0 Kudos
KimGarbade
Occasional Contributor III

DB Authentication connecting as Data Owner. 

I'm running the task in the Scheduler using my Domain login on my desktop. 

0 Kudos
DonMorrison1
Occasional Contributor III

I'm not sure if this will help but maybe worth a try.  I call my .bat files a bit differently from the task scheduler.  In the "Edit Action" panel:

Action: Start a program

Program/script: cmd

Add Arguments: /c <my bat file name>

Start in (optional): <path to my bat file>

I assume you are running the scheduled task on the same PC as where you run the bat file and your are sure you are running under the same Windows user.  Is there any thing non-trivial in your bat file? Maybe you could share that with us too?

KimGarbade
Occasional Contributor III

I don't have a connection to the office right now, but i will share in the morning (EST). Basically the bat file contains the exe call followed by the bat file name argument as shown above with an ECHO OFF before that line and PAUSE after it, but I really like your idea to call cmd and pass the bat as an argument. I can run the bat from the cmd manually and it works fine. I'll give it a go tomorrow morning and keep you posted. 

0 Kudos
KimGarbade
Occasional Contributor III

It runs the script but same result - ListFeatureClasses is still empty.... It must have something to do with ArcPy running in scheduler and the complete lack of error messaging in scheduler (that I know of)... maybe its erroring out setting the workspace I don't even know it.

0 Kudos