Python 3.5.1 script won't run via Windows Task Scheduler when user logged off

16274
79
Jump to solution
06-23-2017 09:14 AM
ShelbyZelonisRoberson
Occasional Contributor III

I have a python 3.5.1 script that uses ArcGIS Pro 1.4.1 to export some layouts to PNGs. The script runs with no problem from Windows Task Scheduler when I check the option "Run only when user is logged on", however, I really need to run the script when I am logged off.  

I'm confused because I have other Python scripts (version 2.7) that run on the same server/user account via Task Scheduler while I am logged off with no problem. I've set them up with the exact same properties as my python 3.5.1 script... the only difference is the path to the python and the fact that the other scripts use ArcMap and not ArcGIS Pro. 

Has anyone else had this issue and found a way to solve it? I've set up everything using a service account on a server with admin privileges. I have the properties in Task Scheduler set to run with the highest privileges as well. 

79 Replies
ShelbyZelonisRoberson
Occasional Contributor III

Thanks Kevin - any testing is greatly appreciated. I've tried it with both Named User (and I've made sure that Pro automatically signs me in) and Concurrent licensing and get the same results each time. I'm not using my specific domain account but a domain account that was specifically set up to do things like schedule tasks on servers. I use the same account to run the Python 2.7/ArcMap scripts with no issue.

0 Kudos
ShelbyZelonisRoberson
Occasional Contributor III

Another update... I installed Pro Beta 2.0 and upgraded my license manager. I tried running this using Pro 2.0 both concurrent and named user, but I'm still having the same issue.

The account I'm using is in the local administrators group on the server. Kevin - are there any permissions things you can think of that might be causing the issue? Doesn't really explain why it would work with the ArcMap/Python 2.7 script though.

0 Kudos
KevinHibma
Esri Regular Contributor

Being in the administrators group should be enough - that is how I do it.

You've said "it doesn't work". Maybe we need to explore what that means - whether the task is being run and failing, or whether the task doesn't run at all.

Could you take the following code and turn this into a schedule task? You'll need to update the path to a project that exists, with a map and has a single layer. The thought here is, you'll get a text file with the messages from the get count tool. If that works, you know there is no license problem and the schedule task work - its a problem with the export. Otherwise if this fails to produce a txt file, then it could still be a license problem. At that point I'd check to see what messages you have in the event viewer or the task scheduler itself.

import arcpy

p = arcpy.mp.ArcGISProject("C:\\arcgisserver\\documents\\GPService\\ProGPPDS.aprx")
maps = p.listMaps()[0]
layers = maps.listLayers()[0]

gc = arcpy.GetCount_management(layers)

f = open("c:/temp/output.txt", 'w')
f.write(gc.getMessages())
f.close()
0 Kudos
ShelbyZelonisRoberson
Occasional Contributor III

I really appreciate all the help. I took your code, pointed to a project with one layer, and ran it manually. It resulted in a text file with a record count, as expected.

Next, I scheduled your script as a task with the same settings that I was trying with my own script. The issues I am experiencing still happened with your script. When I check the box "run whether user is logged on or not" and log off (or even stay logged on), after the scheduled time passes the task scheduler says "The operation completed successfully". However, this is only after a few seconds of running and the the expected export (PNG in my case, text file in your example) is not produced. It seems as if the script never actually runs even though the task says it has completed. 

When I change the setting to "Run only when user is logged on", the task completes successfully and it results in a text file of a record count, as expected. 

Just to note: I've also tried this by pointing to propy as the Program/script instead of python.exe (as recommended here: Python in ArcGIS Pro—ArcPy Get Started | ArcGIS Desktop). Still run into the same behavior, though.

0 Kudos
MichaelVolz
Esteemed Contributor

Shelby - Do you have the ability to get access to Server 2016 to perform this testing?

0 Kudos
ShelbyZelonisRoberson
Occasional Contributor III

Unfortunately I don't, all of ours are 2012.

0 Kudos
KevinHibma
Esri Regular Contributor

Hi Shelby,

I was made aware of the fact that yes, indeed there is a difference between 1.4 and the upcoming release of 2.0. You cannot perform the export from a scheduled task. This is documented here:


If you create a task to run when the user is logged off of the Windows machine, then the Layout Class export functions (for example, exportToPDF) and the Bookmark Class updateThumbnailfunction will not work. These functions require a user to be logged on to Windows in order to work”

 

http://pro.arcgis.com/en/pro-app/arcpy/mapping/guidelines-for-arcpy-mapping.htm

They've overcome this for 2.0, thus you should be able to export from a scheduled task.

Now that said...the fact you can't run the test script I gave you (of running a gp tool)...that narrows down the problem a little bit. Could you update it to run this? If this does not create a text file with anything, not even "this is before arcpy"...then this has nothing to do with licensing or arcpy and its something to do with running python through your scheduled task. If it creates the textfile and outputs some lines...well depending how far it gets, that tells something else.

f = open("c:/temp/output.txt", 'w')
f.write("this is before arcpy \n")
import arcpy
f.write("this is after arcpy \n")

p = arcpy.mp.ArcGISProject("C:\\arcgisserver\\documents\\GPService\\ProGPPDS.aprx")
maps = p.listMaps()[0]
layers = maps.listLayers()[0]
f.write("this is before the tool\n")
gc = arcpy.GetCount_management(layers)
f.write(gc.getMessages())
f.close()

I realize your final goal is to export from a scheduled task. As mentioned, unfortunately you wont be able to do that in 1.4. But we can continue to troubleshoot and try to figure out why you cant run a simple scheduled task. If you get that figured out, then once Pro 2.0 is available you'll be set to export.

curtvprice
MVP Esteemed Contributor

Kevin, this is the test we were doing. This gave us an error message from importing ArcPy. (UPDATED)

# test.py
print("Before ArcPy")
import arcpy
print("After ArcPy")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

# test.bat for Scheduled Tasks, ArcGIS 10.4
echo Start: %DATE% %TIME% >test_out.txt
C:\Python27\ArcGIS10.4\python.exe test.py >>test_out.txt 2>&1‍‍‍‍‍‍‍‍‍‍‍‍‍

I suppose with Pro this would work:

# test.bat for Scheduled Tasks, ArcGIS Pro
setlocal
echo Start: %DATE% %TIME% >test_out.txt
set PROPY=C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.bat
cmd /c "%PROPY%" test.py >>test_out.txt 2>&1‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
ShelbyZelonisRoberson
Occasional Contributor III

Hi Kevin -- I tested this script this morning.  When I scheduled and ran the task, the output file was updated but it only says "this is before arcpy". 

0 Kudos
KevinHibma
Esri Regular Contributor

Ok, that gets us somewhere. I'm guessing its probably a licensing issue.

One more test? The following will capture and exception caused by trying to import arcpy. Hopefully this provides a helpful message to see whats going on.

f = open("c:/temp/output.txt", 'w')
f.write("this is before arcpy \n")
try: 
 import arcpy
 f.write("arcpy imported\n")
except Exception as e :
 f.write(str(e))
 
f.close()
0 Kudos