Python Version: 2.1
Platform: HP Windows XP

I am using Python scripts generated by ArcGIS/ModelBuilder in order to automate geoprocesses. Currently I am calling a series of scripts from a batch file that is called by the task scheduler. My problem is if there is an error the process stops, returns to the batch file and the next script starts. What I need to do is pass a result code to the batch file so I can tell if the script executed correctly. The scripts are independent and do not rely on the previous script but I still need to know if one of them failed. Any help would be greatly appreciated. Thanks

Dani AI

Generated

As posted the generated ArcGIS script above and asked for a concrete example, the simplest reliable approach is: make the Python script explicitly exit with a status code on success or failure, and have the batch file check %ERRORLEVEL% immediately after the Python run. That way the scheduled task can run a single batch that records which scripts succeeded and which failed.

A minimal pattern for the Python side (wrap your GP work; print or log the GP messages and stack trace, then exit non‑zero on error):

import sys, traceback

try:
    # perform geoprocessing work here
    pass
except Exception:
    traceback.print_exc()           # send stack trace to console/log
    # if your geoprocessor object exposes messages, print them too
    sys.exit(1)                      # nonzero = failure
else:
    sys.exit(0)                      # zero = success

On the batch file side call python.exe explicitly (not pythonw.exe) and check %ERRORLEVEL%:

C:\Python21\python.exe "C:\path\do_task.py"
IF %ERRORLEVEL% NEQ 0 (
  echo do_task.py FAILED with %ERRORLEVEL% >> C:\logs\gp_run.log
  REM optionally goto :stop or take recovery steps
) ELSE (
  echo do_task.py succeeded >> C:\logs\gp_run.log
)

Troubleshooting tips: always call python.exe (file associations can use pythonw and lose console/exit behavior); use traceback or the logging module to capture details to a file because Task Scheduler runs without an interactive console; if ArcGIS geoprocessing returns its own messages, include them in the log output; and if you want the batch to stop on first failure, jump to a failure label when %ERRORLEVEL% is nonzero. This pattern makes automation deterministic and gives you actionable logs when a script fails.

Recommended Answers

All 2 Replies

You need to post an example code of one of your typical scripts, otherwise I am lost!

You need to post an example code of one of your typical scripts, otherwise I am lost!

Sure here you go.

# ---------------------------------------------------------------------------
# LMARKS_
# Created on: Tue May 24 2005 08:27:58 AM
# (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Set the necessary product code
gp.SetProduct("ArcInfo")

# Load required toolboxes...
gp.AddToolbox("H:/Patrick/P_Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")


# Local variables...
LM_NAC_SELECT_shp = "x:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp"
LM_UNION_TEMP_shp = "x:\\_GIS\\WORKSPACE\\LM_UNION_TEMP.shp"
LM_LMARK_PT_IDENT_shp = "x:\\_GIS\\WORKSPACE\\LM_LMARK_PT_IDENT.shp"
T__COB_Places_LMARK_shp_ = "x:\\COB\\Places\\LMARKS"
LM_NAC_SELECT__3_ = "x:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp"
LM_PD_DIST_SELECT_DIS_shp__3_ = "x:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp"
LM_PD_DIST_SELECT_DIS_shp__4_ = "x:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp"
LM_NAC_SELECT__4_ = "x:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp"
T__IMS_COB_LMARKS_shp_ = "x:\\IMS\\COB\\LMARKS"
LM_LMARK_TEMP_shp__8_ = "x:\\_GIS\\WORKSPACE\\LM_LMARK_TEMP.shp"
LM_PD_DIST_SELECT_shp = "x:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT.shp"
LMARKS_shp__2_ = "x:\\COB\\Places\\LMARKS.shp"
LMARKS_shp__3_ = "x:\\IMS\\COB\\LMARKS.shp"
LM_LMARK_SELECT_shp = "x:\\_GIS\\WORKSPACE\\LM_LMARK_SELECT.shp"
LM_NAC_SELECT__2_ = "x:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp"
LM_PD_DIST_SELECT_DIS_shp__2_ = "x:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp"
LM_PD_DIST_SELECT_DIS_shp = "x:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp"
nac_shp = "x:\\COB\\Boundary\\nac.shp"
pd_dist_shp = "x:\\COB\\Boundary\\pd_dist.shp"
LMARKS_shp = "x:\\COB\\Places\\LMARKS.shp"
LM_LMARK_PT_shp = "S:\\_GIS\\WORKSPACE\\LM_LMARK_PT.shp"
LM_LMARK_SELECT_Layer = "LM_LMARK_SELECT_Layer"
LM_LMARK_SELECT_Layer_ = "LM_LMARK_SELECT_Layer"
LM_LMARK_SELECT_JOIN_shp = "S:\\_GIS\\WORKSPACE\\LM_LMARK_SELECT_JOIN.shp"
LM_LMARK_TEMP_shp__6_ = "S:\\_GIS\\WORKSPACE\\LM_LMARK_TEMP.shp"
LM_UNION_TEMP_shp__2_ = "S:\\_GIS\\WORKSPACE\\LM_UNION_TEMP.shp"
LM_LMARK_SELECT_JOIN_Layer = "LM_LMARK_SELECT_JOIN_Layer"
LM_LMARK_TEMP_shp = "S:\\_GIS\\WORKSPACE\\LM_LMARK_TEMP.shp"
LM_LMARK_TEMP_shp__2_ = "S:\\_GIS\\WORKSPACE\\LM_LMARK_TEMP.shp"
T__IMS_COB_LMARKS_shp_Delete_succeeded = "false"
T__COB_Places_LMARKS_shp_Delete_succeeded = "false"
Delete_succeeded__7_ = "false"
Delete_succeeded__3_ = "false"
Delete_succeeded__5_ = "false"
Delete_succeeded__6_ = "false"
Delete_succeeded__8_ = "false"
Delete_succeeded__2_ = "false"
Delete_succeeded__9_ = "false"
Delete_succeeded = "false"
Delete_succeeded__4_ = "false"

# Process: Select (2)...
try:
gp.Select_analysis(LMARKS_shp, LM_LMARK_SELECT_shp, "")
excpect spew
print 'spew'

# Process: Make Feature Layer...
gp.MakeFeatureLayer_management(LM_LMARK_SELECT_shp, LM_LMARK_SELECT_Layer_, "", "", "LMUID LMUID VISIBLE;NAME NAME VISIBLE;TYPE TYPE VISIBLE;ACTIVE ACTIVE VISIBLE;UPDATED_BY UPDATED_BY VISIBLE;UPDATED_ON UPDATED_ON VISIBLE;NOTES NOTES VISIBLE;PD_DIST PD_DIST VISIBLE;NAC_NAME NAC_NAME VISIBLE")

# Process: Feature To Point...
gp.FeatureToPoint_management(LM_LMARK_SELECT_shp, LM_LMARK_PT_shp, "CENTROID")

# Process: Select...
gp.Select_analysis(nac_shp, LM_NAC_SELECT_shp, "\"ACTIVE\" = 'y'")

# Process: Add Field (3)...
gp.AddField_management(LM_NAC_SELECT_shp, "NAC_NAME", "TEXT", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (4)...
gp.CalculateField_management(LM_NAC_SELECT__3_, "NAC_NAME", "[NAME]")

# Process: Delete Field (2)...
gp.DeleteField_management(LM_NAC_SELECT__4_, "AREA;PERIMETER;NAC_ID;ACRES;NAME;NAME1;TITLE1;PH1;PH1A;ADD1;EMAIL1;NAME2;TITLE2;PH2;PH2A;ADD2;EMAIL2;MTG_DAY;MTG_TIME;MTG_PLACE;COMMENTS;UPDATED_ON;UPDATED_BY;ACTIVE")

# Process: Select (4)...
gp.Select_analysis(pd_dist_shp, LM_PD_DIST_SELECT_shp, "")

# Process: Dissolve...
gp.Dissolve_management(LM_PD_DIST_SELECT_shp, LM_PD_DIST_SELECT_DIS_shp, "DISTRICT", "", "MULTI_PART")

# Process: Add Field (4)...
gp.AddField_management(LM_PD_DIST_SELECT_DIS_shp, "PD_DIST", "TEXT", "", "", "16", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (3)...
gp.CalculateField_management(LM_PD_DIST_SELECT_DIS_shp__3_, "PD_DIST", "[DISTRICT]")

# Process: Delete Field (4)...
gp.DeleteField_management(LM_PD_DIST_SELECT_DIS_shp__4_, "DISTRICT")

# Process: Union...
gp.Union_analysis("S:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp '';S:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp ''", LM_UNION_TEMP_shp, "ALL", "", "GAPS")

# Process: Delete Field (3)...
gp.DeleteField_management(LM_UNION_TEMP_shp, "FID_LM_NAC")

# Process: Identity...
gp.Identity_analysis(LM_LMARK_PT_shp, LM_UNION_TEMP_shp__2_, LM_LMARK_PT_IDENT_shp, "ALL", "", "NO_RELATIONSHIPS")

# Process: Add Join...
gp.AddJoin_management(LM_LMARK_SELECT_Layer_, "LMUID", LM_LMARK_PT_IDENT_shp, "LMUID", "KEEP_ALL")

# Process: Copy Features...
gp.CopyFeatures_management(LM_LMARK_SELECT_Layer, LM_LMARK_SELECT_JOIN_shp, "", "0", "0", "0")

# Process: Make Feature Layer (2)...
gp.MakeFeatureLayer_management(LM_LMARK_SELECT_JOIN_shp, LM_LMARK_SELECT_JOIN_Layer, "", "", "LM_LMARK_S LMUID VISIBLE;LM_LMARK_1 NAME VISIBLE;LM_LMARK_2 TYPE VISIBLE;LM_LMARK_3 ACTIVE VISIBLE;LM_LMARK_4 UPDATED_BY VISIBLE;LM_LMARK_5 UPDATED_ON VISIBLE;LM_LMARK_6 NOTES VISIBLE;LM_LMARK_7 LM_LMARK_7 VISIBLE;LM_LMARK_8 LM_LMARK_8 VISIBLE;LM_LMARK_P LM_LMARK_P VISIBLE;LM_LMARK_9 LM_LMARK_9 VISIBLE;LM_LMAR_10 LM_LMAR_10 VISIBLE;LM_LMAR_11 LM_LMAR_11 VISIBLE;LM_LMAR_12 LM_LMAR_12 VISIBLE;LM_LMAR_13 LM_LMAR_13 VISIBLE;LM_LMAR_14 LM_LMAR_14 VISIBLE;LM_LMAR_15 LM_LMAR_15 VISIBLE;LM_LMAR_16 LM_LMAR_16 VISIBLE;LM_LMAR_17 PD_DIST VISIBLE;LM_LMAR_18 NAC_NAME VISIBLE;LM_LMAR_19 LM_LMAR_19 VISIBLE;LM_LMAR_20 LM_LMAR_20 VISIBLE")

# Process: Copy Features (2)...
gp.CopyFeatures_management(LM_LMARK_SELECT_JOIN_Layer, LM_LMARK_TEMP_shp, "", "0", "0", "0")

# Process: Delete Field...
gp.DeleteField_management(LM_LMARK_TEMP_shp, "LM_LMARK_7;LM_LMARK_8;LM_LMARK_P;LM_LMARK_9;LM_LMAR_10;LM_LMAR_11;LM_LMAR_12;LM_LMAR_13;LM_LMAR_14;LM_LMAR_15;LM_LMAR_16;LM_LMAR_19;LM_LMAR_20;LM_LMAR_21;LM_LMAR_22")

# Process: Add Attribute Index...
gp.toolbox = "H:/Patrick/P_Tools.tbx"
gp.AddIndex(LM_LMARK_TEMP_shp__6_, "LMUID;NAME;PD_DIST;NAC_NAME", "", "NON_UNIQUE", "NON_ASCENDING")

# Process: Add Spatial Index...
gp.AddSpatialIndex_management(LM_LMARK_TEMP_shp__2_, "0", "0", "0")

# Process: Delete (13)...
gp.Delete_management(LMARKS_shp__2_, "T:\\COB\\Places\\LMARKS.shp")

# Process: Copy...
gp.Copy_management(LM_LMARK_TEMP_shp__8_, T__COB_Places_LMARK_shp_, "")

# Process: Delete (12)...
gp.Delete_management(LMARKS_shp__3_, "T:\\IMS\\COB\\LMARKS.shp")

# Process: Copy (2)...
gp.Copy_management(LM_LMARK_TEMP_shp__8_, T__IMS_COB_LMARKS_shp_, "")

# Process: Delete (7)...
gp.Delete_management(LM_LMARK_SELECT_shp, "S:\\_GIS\\WORKSPACE\\LM_LMARK_SELECT.shp")

# Process: Delete (3)...
gp.Delete_management(LM_LMARK_PT_shp, "S:\\_GIS\\WORKSPACE\\LM_LMARK_PT.shp")

# Process: Delete (5)...
gp.Delete_management(LM_LMARK_SELECT_JOIN_shp, "S:\\_GIS\\WORKSPACE\\LM_LMARK_SELECT_JOIN.shp")

# Process: Delete (6)...
gp.Delete_management(LM_LMARK_TEMP_shp__8_, "S:\\_GIS\\WORKSPACE\\LM_LMARK_TEMP.shp")

# Process: Delete (8)...
gp.Delete_management(LM_PD_DIST_SELECT_shp, "S:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT.shp")

# Process: Delete (2)...
gp.Delete_management(LM_PD_DIST_SELECT_DIS_shp__2_, "S:\\_GIS\\WORKSPACE\\LM_PD_DIST_SELECT_DIS.shp")

# Process: Delete (9)...
gp.Delete_management(LM_UNION_TEMP_shp__2_, "S:\\_GIS\\WORKSPACE\\LM_UNION_TEMP.shp")

# Process: Delete...
gp.Delete_management(LM_NAC_SELECT__2_, "S:\\_GIS\\WORKSPACE\\LM_NAC_SELECT.shp")

# Process: Delete (4)...
gp.Delete_management(LM_LMARK_PT_IDENT_shp, "S:\\_GIS\\WORKSPACE\\LM_LMARK_PT_IDENT.shp")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.