9

I want to programmatically access the current job id of a geoprocessing script running on ESRI 10.2. The script is what needs to know the job id, not the caller of the script.

While searching, I have seen plenty of examples of how the submit job request returns with the job id. This isn't what I want.

In my script that is running on the server, I want to include the job id in the logs, however I haven't found how to obtain the job in in the script. I initially checked arcpy.env, but I didn't see anything. Where else should I look?

5
  • 2
    You want the running service to report the jobId? There isn't anything exposed to do this. You could use the 'os' module to parse paths and get it from there.
    – KHibma
    Sep 24, 2013 at 17:20
  • 1
    That is disappointing. I was thinking along the same lines as you suggested; thanks for confirming my suspicions.
    – pgreen2
    Sep 24, 2013 at 17:53
  • @KHibma, is there simple way to determine if the script is running on the server vs through arcmap?
    – pgreen2
    Sep 24, 2013 at 18:07
  • 1
    As Kevin suggested, I'd advice trying to get the job id by looking through the folders that are created within arcgisserver folder / arcgisjobs. You might want to see what folder was created last and get its name. Sep 24, 2013 at 18:09
  • @pgreen2 check out this thread: gis.stackexchange.com/questions/64937/…
    – KHibma
    Sep 24, 2013 at 18:16

2 Answers 2

7

I remembered an old script I use in testing so I thought I'd share. Maybe it'll help you or someone else. I added the logic to get the GUID. There's probably a better way to do it, but it works.

import arcpy, sys, socket, os

theExe = sys.executable

arcpy.AddMessage("the executable    : " + theExe)
arcpy.AddMessage("where is arcpy    : " + str(arcpy.__file__))
arcpy.AddMessage("the install dir   : " + str(arcpy.GetInstallInfo()["InstallDir"]))
arcpy.AddMessage("the product is    : " + str(arcpy.GetInstallInfo()["ProductName"]))
arcpy.AddMessage("the py version is : " + str(sys.version))
arcpy.AddMessage("hostname          : " + str(socket.gethostname()))
arcpy.AddMessage("path              : " + str(sys.path[0]))
arcpy.AddMessage("path              : " + str(os.path.dirname(__file__)))
arcpy.AddMessage("working dir       : " + str(os.getcwd()))
scr = arcpy.env.scratchFolder
arcpy.AddMessage(scr)  
if "server" in theExe.lower():
    guid = os.path.split(os.path.split(scr)[0])[1] #split 'scratch' off, then split remainder and grab guid    
    arcpy.AddMessage(guid)

arcpy.AddMessage("--------------------------------")
arcpy.AddMessage(arcpy.ProductInfo())
arcpy.AddMessage(arcpy.GetInstallInfo())
3

You can also use arcpy.env.scratchWorkspace as it returns:

"e:\arcgisserver\directories\arcgisjobs\analysis\your_program\j83b20a86cc2d471cb684b4ce0aa9b81b\scratch"

To isolate the job id from the string:

job_id = arcpy.env.scratchWorkspace.rsplit("\\", 2)[1]
>>>"j83b20a86cc2d471cb684b4ce0aa9b81b"
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.