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

19567
79
Jump to solution
06-23-2017 09:14 AM
ShelbyZelonisRoberson
Frequent Contributor

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
Frequent Contributor

Hey Kevin -- I ran this script and it produces an empty output file. 

0 Kudos
AdamLake2
Occasional Contributor

Just glancing at that code, you probably are getting an empty output because it never runs the f.close() line if there is an error. Try this instead:

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))
finally: 
 f.close()
0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Hi Adam... when I choose "run whether user is logged on or not" I still get a blank file. It's weird, because the file does get updated at the time the task is scheduled (so the task is running) but it's just blank. When I choose "Run only when user is logged on", I get the appropriate text in the text file and everything works as it should.

0 Kudos
AdamLake2
Occasional Contributor

If it has an empty output and doesn't even have the "this is before arcpy" line in the text file, then it's not an arcpy/Pro issue. Check the permissions on the python folders and the output folders. Try logging into the machine as the user that it is scheduled to run as instead of your own user and see if the script works then. That will help narrow down the issue.

0 Kudos
mpboyle
Frequent Contributor

I was pointed to this thread because I'm having a similar issue.  I have a python script that is exporting PDFs from an ArcGIS Pro project.  The script works as expected using an IDE (PyCharm) but when I try to migrate it to task scheduler, it will ONLY work when I have 'Run only when user is logged on'.  If I change this property to 'Run whether user is logged on or not' it will fail.

The system environment the script is being run on:

OS: Windows Server 2012 R2

Pro Version: ArcGIS Pro 2.0

Pro License Type: Single Use Advanced

Python exe: ..\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

So...if I'm reading the thread above correctly...starting at Pro 2.0 you should be able to export out documents (PDF, PNG, JPG, etc...) from a layout...which again, I can confirm is doable, but ONLY if the task is set to 'Run only when user is logged on'.  Is there no way to execute this when the user is not logged on?

0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Yep you summarized everything correctly, though it seems I can't get any Python 3 script using Pro to work (not just exporting but even simple GP tool scripts) when I check the option to run whether I'm logged on or not. Still hoping for a way to get it to work - right now I'm in the same spot as you.

0 Kudos
mpboyle
Frequent Contributor

I agree that somewhere a specific (or all) arcpy function(s) seems to be an issue.  If I comment out all the functions that use arcpy and re-run my script, it executes fine with 'Run whether user is logged on or not'.  The script will successfully create a log (.txt file) and send an email when complete.

ThomasColson
MVP Frequent Contributor

Does the service account have "FULL CONTROL" Permissions to the output folder AND AND AND the Python folder (Not read/write, FULL CONTROL)? Has a GPO update blocked the service account from "Execute Batch Job"? 

0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Hi Thomas -- yes, the service account has Full Control over all folders involved and is not blocked from Execute Batch Job. 

MichaelVolz
Esteemed Contributor

Shelby:

You indicated that Desktop python scripts (2.7) worked successfully on the same server when not logged in, so wouldn't that indicate that permissions are set correctly thus it's a python 3.5.1 issue

0 Kudos