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

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

I would second that it points to python 3.5.  I'm using the same username on the same server to execute both 2.7 and 3.5 scripts.  My 2.7 scripts have no issue running when set to 'Run whether user is logged on or not'.

0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Yep, agreed.

0 Kudos
mpboyle
Frequent Contributor

Just for a test, I created a simple python script (below) that calls some basic arcpy functions to see whether or not it's arcpy in general or some specific functions that are causing an issue.

The script below will output a .txt file to a user's desktop showing whether or not a sample feature class exists and if it does, some basic describe properties.

I was able to execute this script using python 3.5 that comes with ArcGIS Pro and setting Task Scheduler to 'Run whether user is logged on or not' ... so, it may be the actual layout exporting functions that are an issue.

# import modules
import os


userDesktop = os.path.join(os.path.expanduser('~'), 'Desktop')
testFc = r'...path to fc...'


def test_arcpy(output_log_folder, test_fc_path):
    log_path = os.path.join(output_log_folder, 'arcpyTest.txt')
    f = open(log_path, 'w')

    import arcpy

    f.write('Successfully imported arcpy...\n')

    # try testing if fc exists
    if arcpy.Exists(test_fc_path):
        f.write('{0} exists...\n'.format(test_fc_path))

        # try testing some describe functions
        desc = arcpy.Describe(test_fc_path)

        data_type = desc.dataType
        shp_type = desc.shapeType
        oid_fld = desc.OIDFieldName
        sr = desc.spatialReference.name

        f.write('  Data Type: {0}\n'.format(data_type))
        f.write('  Shape Type: {0}\n'.format(shp_type))
        f.write('  OID Field: {0}\n'.format(oid_fld))
        f.write('  Spatial Reference: {0}\n'.format(sr))

    else:
        f.write('{0} does not exist...'.format(test_fc_path))

    f.close()


# test arcpy functions
test_arcpy(userDesktop, testFc)
0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Hi Matthew,

This script works for me also; but it doesn't have to open/call an ArcPro project, right? Maybe that has something to do with it?

0 Kudos
mpboyle
Frequent Contributor

I went through my script line by line, commented out functions, and re-ran through Task Scheduler with 'Run whether user is logged on or not', until it no longer worked.  It finally "failed" when I un-commented the 'exportToPDF' function.  Although, it doesn't really fail, Task Scheduler will still show the Last Run Result as successful, it just doesn't seem to actually export anything.

I tried exportToJPEG and exportToPNG, both with the same result as exportToPDF.

Below is a copy of a log my script outputs when I leave the 'exportToPDF' function commented out.  Even though it may say successfully exported a PDF, it's simply indicating where the PDF should be written to.  You can see the script has no problem accessing the ArcGIS Pro document, updating layer definition queries, turning layers on or off, setting the map frame extent to that of a layer, adjusting the map frame scale, updating layout text elements, etc... it's the 'exportTo...' function that does not work.

Permissions are not an issue.  My log is being written to a different server successfully, and the output PDFs are being written to a different server successfully if I change the Task Scheduler property to 'Run only when user is logged on'.

So...even though previous comments indicated this would be fixed at ArcGIS Pro 2.0, it doesn't appear that it has been.  Exporting layouts through Task Scheduler with 'Run whether user is logged on or not' seems to be a bug, and not possible, at least based on our system environment, shown below.

System Environment

OS: Windows Server 2012 R2

ArcGIS Pro Version: 2.0

ArcGIS Pro License Type: Single Use Advanced

Task Scheduler Action shown below.  I see in the Pro docs it's recommended to use the propy.bat file, so I am, with an argument set to the file path of my script.

Example log file

User: MBoyle
Time: 2017-06-30 10:14:46 AM
Script: E:\GIS-Data\Scripts\DailyReappraisalMaps.py
--------------------------------------------------
2017-06-30 10:14:46 AM: Attempting to truncate ..\Matt\Production\Assessor\Reappraisal\Maps\DailyReappraisal\DailyReappraisal.gdb\asrRprslMapAreaY...
2017-06-30 10:14:48 AM: Successfully truncated ..\Matt\Production\Assessor\Reappraisal\Maps\DailyReappraisal\DailyReappraisal.gdb\asrRprslMapAreaY... (Elapsed time: 1.77 seconds)
2017-06-30 10:14:49 AM: Attempting to load gdb.DBO.svw_asrRprslMapAreaY into ..\Matt\Production\Assessor\Reappraisal\Maps\DailyReappraisal\DailyReappraisal.gdb\asrRprslMapAreaY...
2017-06-30 10:14:54 AM: Successfully loaded 4 rows from gdb.DBO.svw_asrRprslMapAreaY into ..\Matt\Production\Assessor\Reappraisal\Maps\DailyReappraisal\DailyReappraisal.gdb\asrRprslMapAreaY... (Elapsed time: 3.96 seconds)
--------------------------------------------------
2017-06-30 10:14:54 AM: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29 already exists; skipping attempt to create...
--------------------------------------------------
2017-06-30 10:14:58 AM: ArcGIS Pro Project: ..\Matt\Production\Assessor\Reappraisal\Maps\DailyReappraisal\DailyReappraisal.aprx
--------------------------------------------------
2017-06-30 10:14:58 AM: Maps:
2017-06-30 10:14:58 AM:   Reappraisal
--------------------------------------------------
2017-06-30 10:14:58 AM: Layouts:
2017-06-30 10:14:58 AM:   Landscape
2017-06-30 10:14:58 AM:   Portrait
--------------------------------------------------
2017-06-30 10:14:58 AM: Orientations:
2017-06-30 10:14:58 AM:   L
2017-06-30 10:14:58 AM:   P
--------------------------------------------------
2017-06-30 10:14:59 AM: Landscape Features:
2017-06-30 10:14:59 AM:   MapArea=Marion 1040
2017-06-30 10:14:59 AM:   OID=1
2017-06-30 10:14:59 AM:   *~*~*~*~*~*~*~*~*
2017-06-30 10:14:59 AM:   MapArea=Marion 1018
2017-06-30 10:14:59 AM:   OID=2
--------------------------------------------------
2017-06-30 10:14:59 AM: Portrait Features:
2017-06-30 10:14:59 AM:   MapArea=Marion 1024
2017-06-30 10:14:59 AM:   OID=3
2017-06-30 10:14:59 AM:   *~*~*~*~*~*~*~*~*
2017-06-30 10:14:59 AM:   MapArea=Marion 1019
2017-06-30 10:14:59 AM:   OID=4
--------------------------------------------------
2017-06-30 10:14:59 AM: Attempting to update 'Parcels Visited Yesterday' layer name within 'Reappraisal' map to 'Parcels Visited 06/29/2017'...
2017-06-30 10:14:59 AM: Successfully updated 'Parcels Visited Yesterday' layer name within 'Reappraisal' map to 'Parcels Visited 06/29/2017'... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: There are 2 landscape pages to export...
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to turn on 'Assessor Map Area Index (L)' layer within 'Reappraisal' map...
2017-06-30 10:15:08 AM: Successfully turned on 'Assessor Map Area Index (L)' layer within 'Reappraisal' map... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to turn off 'Assessor Map Area Index (P)' layer within 'Reappraisal' map...
2017-06-30 10:15:08 AM: Successfully turned off 'Assessor Map Area Index (P)' layer within 'Reappraisal' map... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to update definition query of 'Assessor Map Area Index (L)' layer to 'OBJECTID=1'...
2017-06-30 10:15:08 AM: Successfully updated definition query of 'Assessor Map Area Index (L)' layer to 'OBJECTID=1'... (Elapsed time: 0.04 seconds)
2017-06-30 10:15:08 AM: Attempting to set 'Data View Map Frame' map frame within 'Landscape' layout to the extent of the 'Assessor Map Area Index (L)' layer...
2017-06-30 10:15:08 AM: Successfully set 'Data View Map Frame' map frame within 'Landscape' layout to the extent of the 'Assessor Map Area Index (L)' layer... (Elapsed time: 0.03 seconds)
2017-06-30 10:15:08 AM: Attempting to apply a 5.0% padding to 'Landscape' layout scale; setting layout scale to 1:8,965...
2017-06-30 10:15:08 AM: Successfully applied a 5.0% padding to 'Landscape' layout scale; setting layout scale to 1:8,965... (Elapsed time: 0.01 seconds)
2017-06-30 10:15:08 AM: Attempting to update 'Landscape' layout 'MapArea' text element to 'Marion 1040'...
2017-06-30 10:15:08 AM: Successfully updated 'Landscape' layout 'MapArea' text element to 'Marion 1040'... (Elapsed time: 0.00 seconds)
2017-06-30 10:15:08 AM: Attempting to export 'Landscape' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1040.pdf...
2017-06-30 10:15:08 AM: Successfully exported 'Landscape' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1040.pdf... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to update definition query of 'Assessor Map Area Index (L)' layer to 'OBJECTID=2'...
2017-06-30 10:15:08 AM: Successfully updated definition query of 'Assessor Map Area Index (L)' layer to 'OBJECTID=2'... (Elapsed time: 0.04 seconds)
2017-06-30 10:15:08 AM: Attempting to set 'Data View Map Frame' map frame within 'Landscape' layout to the extent of the 'Assessor Map Area Index (L)' layer...
2017-06-30 10:15:08 AM: Successfully set 'Data View Map Frame' map frame within 'Landscape' layout to the extent of the 'Assessor Map Area Index (L)' layer... (Elapsed time: 0.03 seconds)
2017-06-30 10:15:08 AM: Attempting to apply a 5.0% padding to 'Landscape' layout scale; setting layout scale to 1:5,612...
2017-06-30 10:15:08 AM: Successfully applied a 5.0% padding to 'Landscape' layout scale; setting layout scale to 1:5,612... (Elapsed time: 0.01 seconds)
2017-06-30 10:15:08 AM: Attempting to update 'Landscape' layout 'MapArea' text element to 'Marion 1018'...
2017-06-30 10:15:08 AM: Successfully updated 'Landscape' layout 'MapArea' text element to 'Marion 1018'... (Elapsed time: 0.00 seconds)
2017-06-30 10:15:08 AM: Attempting to export 'Landscape' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1018.pdf...
2017-06-30 10:15:08 AM: Successfully exported 'Landscape' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1018.pdf... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: There are 2 portrait pages to export...
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to turn on 'Assessor Map Area Index (P)' layer within 'Reappraisal' map...
2017-06-30 10:15:08 AM: Successfully turned on 'Assessor Map Area Index (P)' layer within 'Reappraisal' map... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to turn off 'Assessor Map Area Index (L)' layer within 'Reappraisal' map...
2017-06-30 10:15:08 AM: Successfully turned off 'Assessor Map Area Index (L)' layer within 'Reappraisal' map... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to update definition query of 'Assessor Map Area Index (P)' layer to 'OBJECTID=3'...
2017-06-30 10:15:08 AM: Successfully updated definition query of 'Assessor Map Area Index (P)' layer to 'OBJECTID=3'... (Elapsed time: 0.04 seconds)
2017-06-30 10:15:08 AM: Attempting to set 'Data View Map Frame' map frame within 'Portrait' layout to the extent of the 'Assessor Map Area Index (P)' layer...
2017-06-30 10:15:08 AM: Successfully set 'Data View Map Frame' map frame within 'Portrait' layout to the extent of the 'Assessor Map Area Index (P)' layer... (Elapsed time: 0.03 seconds)
2017-06-30 10:15:08 AM: Attempting to apply a 5.0% padding to 'Portrait' layout scale; setting layout scale to 1:4,046...
2017-06-30 10:15:08 AM: Successfully applied a 5.0% padding to 'Portrait' layout scale; setting layout scale to 1:4,046... (Elapsed time: 0.01 seconds)
2017-06-30 10:15:08 AM: Attempting to update 'Portrait' layout 'MapArea' text element to 'Marion 1024'...
2017-06-30 10:15:08 AM: Successfully updated 'Portrait' layout 'MapArea' text element to 'Marion 1024'... (Elapsed time: 0.00 seconds)
2017-06-30 10:15:08 AM: Attempting to export 'Portrait' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1024.pdf...
2017-06-30 10:15:08 AM: Successfully exported 'Portrait' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1024.pdf... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:08 AM: Attempting to update definition query of 'Assessor Map Area Index (P)' layer to 'OBJECTID=4'...
2017-06-30 10:15:08 AM: Successfully updated definition query of 'Assessor Map Area Index (P)' layer to 'OBJECTID=4'... (Elapsed time: 0.04 seconds)
2017-06-30 10:15:08 AM: Attempting to set 'Data View Map Frame' map frame within 'Portrait' layout to the extent of the 'Assessor Map Area Index (P)' layer...
2017-06-30 10:15:08 AM: Successfully set 'Data View Map Frame' map frame within 'Portrait' layout to the extent of the 'Assessor Map Area Index (P)' layer... (Elapsed time: 0.03 seconds)
2017-06-30 10:15:08 AM: Attempting to apply a 5.0% padding to 'Portrait' layout scale; setting layout scale to 1:4,815...
2017-06-30 10:15:08 AM: Successfully applied a 5.0% padding to 'Portrait' layout scale; setting layout scale to 1:4,815... (Elapsed time: 0.01 seconds)
2017-06-30 10:15:08 AM: Attempting to update 'Portrait' layout 'MapArea' text element to 'Marion 1019'...
2017-06-30 10:15:08 AM: Successfully updated 'Portrait' layout 'MapArea' text element to 'Marion 1019'... (Elapsed time: 0.00 seconds)
2017-06-30 10:15:08 AM: Attempting to export 'Portrait' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1019.pdf...
2017-06-30 10:15:08 AM: Successfully exported 'Portrait' layout to: ..Some-Folder\Reappraisal\In-House\Reports\Maps\2017-06-29\Marion 1019.pdf... (Elapsed time: 0.00 seconds)
--------------------------------------------------
2017-06-30 10:15:10 AM: End time of script (Elapsed time: 24.06 seconds)
--------------------------------------------------
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ShelbyZelonisRoberson
Frequent Contributor

Thanks. My script is trying to use the exportToPNG so this would make sense. I'm using the same system environment as you, except that I've tried with both Pro 2.0 Named User (Basic) and Concurrent (Advanced) licensing to no avail. 

0 Kudos
mikeking1
Occasional Contributor

If the script is on a server - it most likely can't access your user context when you are not physically logged on.   This could be windows or ArcGIS related.  It sounds like you can schedule other jobs so its probably ArcGIS related.  Check the action of your script carefully for any in memory or temporary layers - unless you explicitly declare the location of these they will often default to areas a service account may not have access to - so the problem is not immediately apparent.

Unfortunately the only way I discovered this was to use the approach outlined above.  Pull your script back to "import arcpy - pass" and then add in blocks until the problem is raised.  It can be pretty hard to trap the problem if/when there is a windows conflict clobbering all of your python error handling.  Good luck!

0 Kudos
RhettZufelt
MVP Notable Contributor

I've had this same issue in the past with any scheduled task. Not at the office to look up what exactly it was, but we are on a domain, and the group policy has a setting that is something like "not allow passwords to be set over network" that had to be disabled in order to run any scheduled tasks when not logged in.

Don't know if this applies in your case, but it is what stopped me from being able to do it in the past.

R_

0 Kudos
ShelbyZelonisRoberson
Frequent Contributor

Mike/Rhett -- thanks, but I can schedule other tasks (ones that use ArcMap/Python 2.7) with absolutely no problem using the exact same settings and service account, and I have no in memory layers either. 

0 Kudos
JeffMoulds
Esri Contributor

In regards to the ArcGIS Pro issue of not being able to use the arcpy.mp Layout Class export functions (for example, exportToPDF) using the operating system's Task Scheduler when the user is logged off of the Windows machine, we have a bug for that. The tracking number is BUG-000106659. There is also a related issue where you will see the similar behavior with Windows Services. The tracking number for that is BUG-000106464. As noted previously in this thread, the issues are not reproducible on all machines. We are currently investigating this issue. When I have more information, I will update this post.

This limitation was noted in the arcpy.mp help: http://pro.arcgis.com/en/pro-app/arcpy/mapping/guidelines-for-arcpy-mapping.htm. Scroll to the bottom of the help topic to the section entitled "Run Scripts as Scheduled Tasks".