Start ArcGIS server Map services using python script & used in automation jobs

4822
8
08-11-2016 12:19 AM

Start ArcGIS server Map services using python script & used in automation jobs

Hello All ESRI users,

I have made some customisation and enhancement to  add new logging functionality in ESRI provided sample script.

With the help of this script we can also track our ArcGIS server map services and generates alerts for any stopped services.

This Script used in windows task scheduler and scheduled on give interval to start services which are failed to start after recycling time.

For info and suggestions please contact me.

Comments

I had issues implementing httpConn.getresponse() with our secured (https) ArcGIS Server sites.  So, I simplified things and just load a JSON object and implement the "realTimeState" attribute for determining services that are stopped/started.  Just pass in the token to the following def():

def reviewRunning(token):
   
    query_dict = { "f": "json", "token": token['token'] } 
     
    # query your services url 
    jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))

    rooturl = agsurl + site + "/admin/services"
    rootresp = json.loads(urllib.urlopen(rooturl + "?f=json", urllib.urlencode(query_dict)).read())

    fldrs = rootresp['folders']
    for fldr in fldrs:
       fldrurl = agsurl + site + "/admin/services/" + fldr 
      
       query_dict = { "f": "json", "token": token['token'] }  
       svcresp = json.loads(urllib.urlopen(fldrurl + "?f=json", urllib.urlencode(query_dict)).read())
       svcs = svcresp['services']
       for svc in svcs:
           svcname = svc['serviceName']
           svctype = svc['type']
          
           query_dict = { "f": "json", "token": token['token'] }
          
           svcstatusurl =  fldrurl + "/" + svcname + "." + svctype + "/status"
           svcstatusresp = json.loads(urllib.urlopen(svcstatusurl + "?f=json", urllib.urlencode(query_dict)).read())
          
           svcstatus = svcstatusresp['realTimeState']
           print svcname, svctype, svcstatus

Anonymous User

Hello James,

Thanks for your valuable feedback and suggestion.

After the above changes its work fine for you that's great news for me

I have just started working on some ArcGIS admin configuration task.

It works to track the ArcGIS server map services and generates alerts.

Anonymous User

No its starts stopped service in ArcGIS server folder and in between if any map services haven't starts.

then this script alerts admin users mail ID.

Excellent script - excellent idea ! Thank you !

Any script automated or scheduled to restart a particular service (stop and then start in case of rebuilding geocoders for example)

Thank you

.....for example I did successfully run the tool (from the admin toolkit) in catalog to execute this pasted here below as python snippet

...........................................

arcpy.StopStartService(Server_Name="xx", Port="6080", Admin_User="xx", Admin_Password="xx", Stop__Start__Delete="Stop", Service_List="xx")

......................................

but, whenever trying to run that as an independent python script so I could schedule it as a task it fails and gives this error

Message File Name Line Position 
Traceback    
    <module> C:\Users\gvoicu\ServerAdminToolkit\StopService.py 6  
AttributeError: 'module' object has no attribute 'StopStartService'    

Thank you

I use somthing like this when ArcGis Server have schema lock enable.

It is good when is performing database sync

import arcpy
username = "UserName"
password = "Password"
token_url = '<serverweblink>/arcgis/admin/generateToken'
urlService ='<serverweblink>/arcgis/admin/services/<service_name>.MapServer/stop?f=pjson&token='
def disconnect_ArcGisServer(username, password, token_url, urlService):
print("###########################################Disconnect Arcgis Server##########################################")
data = {'username': username, 'password': password, 'client': 'requestip'}
token_request = requests.post(url=token_url, data=data)
if token_request.status_code == 200:
print('Success!')
elif token_request.status_code == 404:
print('Not Found.')
token_request.encoding = 'utf-8'
token_text = token_request.text
print("Token Values is " + token_text)
requests.post(url = str(urlService) + token_text)

disconnect_ArcGisServer(username, password, token_url, urlService)

  

For start just replace stop from urlService

Variable will look like that 

urlService ='<serverweblink>/arcgis/admin/services/<service_name>.MapServer/start?f=pjson&token='
Version history
Last update:
‎08-11-2016 12:19 AM
Updated by:
Anonymous User
Contributors