Task scheduler with batch file running python script

2136
12
Jump to solution
10-14-2021 07:55 AM
JamesStout101
New Contributor II

Have created a script and a batch file to run the process automatically.

Pywin opens the script does not" 

Any help in getting it to open and execute would be appreciated.

 

Batch file as follows

C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\Pythonwin.exe
C:\Users\jstout\Desktop\PY\MorningProcess\morningprocess.py
Timeout 15

 

Script as follows

import arcpy, arceditor, time, smtplib

# Set the workspace
arcpy.env.workspace = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'

# Set a variable for the workspace
adminConn = arcpy.env.workspace


database = "Database Connections/toolboxDEFAULTVersion.sde"
versions = arcpy.ListVersions(adminConn)

# Print the versions available to the user
for version in versions:
print(version)
print "Version list complete."

# Execute the ReconcileVersions tool.
# Use a list comprehension to get a list of version names where the owner
# is the current user and make sure sde.default is not selected.
print 'Reconcile versions started.'

verList = [ver.name for ver in arcpy.da.ListVersions() if ver.isOwner
== True and ver.name.lower() != 'sde.EFAULT']

arcpy.ReconcileVersions_management(adminConn, "ALL_VERSIONS", "SDE.DEFAULT", "", "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST","DELETE_VERSION")

# Write messages to a Text File
txtFile = open("c:\\temp\\GPMessages.txt","w")
txtFile.write (arcpy.GetMessages())
txtFile.close()

print ('see error log at "c:\temp\GPMessages.txt"')
print'Reconciling Complete'

# Run the compress tool.
print("Running compress")
arcpy.Compress_management(adminConn)

# Use a connection file to create the connection
print "Check state ID"
egdb = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'
egdb_conn = arcpy.ArcSDESQLExecute(egdb)

table_name = 'EED_FM.sde.sde_States'
field_name = 'state_id'

sql = '''
SELECT {0}, COUNT({0}) AS f_count FROM {1}
GROUP BY {0}
ORDER BY f_count DESC
'''.format(field_name, table_name)

egdb_return = egdb_conn.execute(sql)
for i in egdb_return:
print('{}: {}'.format(*i))
print "State count complete"

# Update statistics and indexes for the system tables
# Note: to use the "SYSTEM" option the user must be an geodatabase or database administrator.
# Rebuild indexes on the system tables
print("Rebuilding indexes on the system tables")
arcpy.RebuildIndexes_management(adminConn, "SYSTEM")

# Update statistics on the system tables
print("Updating statistics on the system tables")
arcpy.AnalyzeDatasets_management(adminConn, "SYSTEM")

print("Finished.")

0 Kudos
1 Solution

Accepted Solutions
DominicRoberge2
Occasional Contributor

Perhaps you could try this format in your BAT file:

 

"C:\Python27\ArcGIS10.6\python.exe" "C:\Users\jstout\Desktop\PY\MorningProcess\morningprocess.py"

View solution in original post

12 Replies
jcarlson
MVP Notable Contributor

Does the script run manually? Are you getting any error messages?

- Josh Carlson
Kendall County GIS
0 Kudos
JamesStout101
New Contributor II

The script does run manually. I can open it in py win and run it runs ok. The problem is making it run on a scheduled task.

0 Kudos
jcarlson
MVP Notable Contributor

I am not familiar with py win, so I'm not sure how that may or may not be playing into things.

When your batch script calls the .py file, how does py win know to look for that? Should the script's filepath be an argument on the same line, after opening the program

For what it's worth, we use conda, and schedule our scripts as follows:

call c:\ProgramData\Anaconda3\Scripts\activate.bat
conda activate <env-name> && python <script-filepath>
- Josh Carlson
Kendall County GIS
JamesStout101
New Contributor II

Placing the argument on same line opened the script it does not execute.

PyWin and the script both open task doesn't run.

0 Kudos
DominicRoberge2
Occasional Contributor

Perhaps you could try this format in your BAT file:

 

"C:\Python27\ArcGIS10.6\python.exe" "C:\Users\jstout\Desktop\PY\MorningProcess\morningprocess.py"

JamesStout101
New Contributor II

Command prompt sits there nothing runs. The command shows in the window as above.

0 Kudos
DominicRoberge2
Occasional Contributor

humm interesting. Can You open the py file using the IDLE try to run it see if you are getting any error messages. (right click on the file and select Edit with IDLE). I use this all the time and it works on desktop and servers

DominicRoberge2_0-1634232690070.png

 

0 Kudos
JamesStout101
New Contributor II

Error

Traceback (most recent call last):
File "C:\Users\jstout\Desktop\PY\MorningProcess\MorningProcess.py", line 1, in <module>
import arcpy, arceditor, time, smtplib
ModuleNotFoundError: No module named 'arcpy'

0 Kudos
DominicRoberge2
Occasional Contributor

 

look at this link in the Paths and import section

https://desktop.arcgis.com/en/arcmap/10.6/analyze/python/importing-arcpy.htm

 

 

DominicRoberge2_0-1634234753683.png

 

0 Kudos