Trying to run a model / script as a scheduled task

3127
12
06-12-2012 08:34 AM
SharonZastre
New Contributor III
Hello,

I have created a model that will Analyze, Sync, and Compress data within a SQL Enterprise geodatabase (ArcGIS 10 SP4). The model runs fine in ArcCatalog. I exported the model to a python script and it runs fine in ArcCatalog as well. But when I run the python script as a scheduled task it seems to run but the data doesn't sync between the databases. I also tried the option of setting up a scheduled task to run a python script / bat file that calls the model itself. This doesn't seem to process properly. Not sure what I am missing but I'm not familiar with python so I don't really know where to begin. Any ideas are appreciated.

Thanks,
Sharon
Tags (2)
0 Kudos
12 Replies
MathewCoyle
Frequent Contributor
If you are executing the script from the system it requires UNC paths as opposed to mapped drives if you are referencing those.
0 Kudos
MichaelVolz
Esteemed Contributor
What operating system are you running the script from (Windows 2003 Server, Windows 2008 Server, other)?

You might want to try calling the python script from a bat file.

You might need to specify the full path to the python exe and the python script if you are not using an environmental variable to specify the location of the python exe.
0 Kudos
ShaunWeston
Occasional Contributor
Normally what I do is create a model in ArcMap/Catalog then export it out to python code, then setup a .bat file that references the python code to run at a particular time. This seems to work best for me, I did try setting up a batch file that references a model directly, but it wouldn't quite work correctly and I wanted to add some logging stuff too.

So anyway once you have exported your script out, you need to make sure that the paths to the data are all correct and the python script runs from ArcMap/Catalog correctly. After this script is setup you need to create a .bat file and then put a line of code like this:
c:\python26\arcgis10.0\python �??c:\script\SimpleModel.py�?�
This will open up python and then refers to the script to run.

After this is setup, all you need to do is setup a basic task in Windows scheduler to run this bat file at a specific time.

What part of the process is failing for you, you may want to put some logging in your script to see where it is failing.
0 Kudos
SharonZastre
New Contributor III
Thank you for all the replies.  The operating system is Windows Server 2008 R2 Standard.  I have been able to run the model in ArcCatalog.  I exported the model to a python script and I can run the script in ArcCatalog as well.  The databases involved get synced as expected.  I have set the Environment Variable PYTHONPATH = C:\Python26\ArcGIS10.0\python.exe and I have called the Toolbox using the mapped letter drive and the UNC path but neither worked.

With trying to run the model or script as a scheduled task I have tried a few different scenarios.

1 - Run the model.  I created a python script that should run the model according to other research I have done.  I also saved this as a batch file but it didn't work either.
     import arcpy
     arcpy.ImportToolbox(r"G:\Tools\Models\DrydenModels.tbx", "TBX")
     arcpy.AnalyzeSyncCompressPWProdPub_TBX()


2 - Run the exported python script.  This appears to at least process because it takes some time for the scheduled task to complete but the databases do not sync.  Attached is the exported python script

3 - Run a batch file that calls the exported python script.
     C:\Python26\ArcGIS10.0\python.exe "C:\Scheduled Tasks\AnalyzeSyncCompressPWProdPub.py"

At this point, I don't know what else to try.  I think I may need to edit the python script so that it can find the databases but exactly how to do that I'm not sure.  It at least works as a model and script within ArcCatalog but ideally I would like it to work as a scheduled task.  Thanks again for all your help.
0 Kudos
NickHetrick
Occasional Contributor II
I looked over your script and something that may be an issue is that you are using default paths to your database connections Does the user account that is running the scirpt have the same exact database connections in its profile. If not the script would fail. I would also verify that the account running the script has access to the correct file locations. Also are you storing the password with the scheduled task this also may be needed as well if the account is accessing remote resources.
0 Kudos
curtvprice
MVP Esteemed Contributor
I have set the Environment Variable PYTHONPATH = C:\Python26\ArcGIS10.0\python.exe


I think you have a misunderstanding of PYTHONPATH. It's the system PATH that locates python.exe, and if ArcGIS scripts are working okay interactively, that PATH is probably set correctly. If you want to ensure you are running a certain python.exe, the easiest way is to use the full path to python.exe in your launching .bat file.

PYTHONPATH is used for something else: to modify the Python sys.path (where Python looks for imports):

Python" rel="nofollow" target="_blank">http://docs.python.org/tutorial/modules.html#the-module-searc... 2.7.3 tutorial: The Module Search Path
0 Kudos
DanaDiotte1
New Contributor II
0 Kudos
ChristopherThompson
Occasional Contributor III
0 Kudos
KenCarrier
Occasional Contributor III
If you are running an analyze on your entire database which it appears you might be, why not use 1 database connection as the SDE user and use some list functions?

This simplistic and only meant to illustrate:

import arcpy
from arcpy import env
env.workspace = ... # your sde connection file as the SDE user

ds = LisDataSets()
for d in ds:
   Analyze(d)
fcs = ListFeatureClasses()
for fc in fcs:
   Analyze(fc)  
tbls = ListTables()
for tbl in tbls:
   Analyze(tbl)


then inside each list call the analyze tool and pass in the parameters from the lists, just a thought.
0 Kudos