ArcGIS Server Map Services – Starting and Stopping ArcGIS Server Map Services Using Python

arcgis-serverpython

Is it possible to programmatically stop and start ArcGIS Server map services using Python?

Best Answer

For those that are using ArcGIS 10.0, and interested in using AGSSOM as recommended by D.E. Wright, I've put together the following code. This will stop all services allowing you to perform necessary geodatabase tasks, and then restart previously running services.

import os, subprocess

#Get list of currently running map services
stream = os.popen('D:\GIS\Admin\AGSSOMv10.0\AGSSOM.exe -list')
mapservices = stream.readlines()
lstStarted = []

for item in mapservices:
    itemSplit = item.split(',')
    if len(itemSplit) > 1:
        if itemSplit[1].find("Started.") > -1:
            lstStarted.append(itemSplit[0])

#Shutdown all map services
subprocess.call(['D:\GIS\Admin\AGSSOMv10.0\AGSSOM.exe', '-x', '*all*'])

##...Perfom geodatabase tasks here

#Restart previously running map services
for mapservice in lstStarted:
    subprocess.call(['D:\GIS\Admin\AGSSOMv10.0\AGSSOM.exe', '-s', mapservice])
Related Question