Is it possible to programmatically stop and start ArcGIS Server map services using Python?
6 Answers
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])
At ArcGIS 10.0, for individual services, such as you could command-line to maintain ArcIMS, there is a commandline tool you can download from the Code Gallery (AGSSOM) that gives you the core of major areas but there is no granular 'service specific' level control that you can use at this time to Start/Stop/Refresh/Add/Remove AGS services.
There is on ideas.arcgis.com several places with requests to improve control of services from a more automatable location.
-
The link to ArcGIS Ideas is broken. In any event, perhaps this answer can be updated to 10.7.– PolyGeo ♦Commented May 9, 2019 at 21:33
As of ArcGIS Server 10.3, several Python-based command line utilities come bundled with ArcGIS server.
Per esri's documentation they are usually installed here:
C:\Program Files\ArcGIS\Server\tools\admin
The manage service utility is great for starting, stopping or deleting services:
python manageservice.py -u admin -p admin -s http://gisserver.domain.com:6080 -n Beirut/Parcels -o start
In the same vein as what Jeff Berry posted in his answer, I wrote a small function that will manage ArcGIS Server 10.0 map services using the AGSSOM tools.
In my case I call services individually by name, stopping and starting one at a time.
import subprocess
def manageMapServices(command, svc, svcType):
PathToAGSSOM = r"C:\Windows\AGSSOMUtils\AGSSOM"
server = "AGSSOM"
p = subprocess.Popen(PathToAGSSOM + " " + server + " " + command + " " + svc + " " + svcType)
p.wait()
Then, you can call a service by name using -x to stop and -s to start:
manageMapServices("-s", "Folder/Service", "MapServer")
For ArcGIS 10.7, and some earlier versions, a complete set of python scripts to stop, start, delete and a bunch of other things can be found here: http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340 (there is also a .exe inside this if you absolutely need a full executable)
Or dive into the help and read about the many options (out of the box command line, python api, code snippets) here: https://enterprise.arcgis.com/en/server/latest/administer/linux/scripting-arcgis-server-administration.htm
However, all of these points are included as other answers in the question you referenced. AGSSOM was a .net utility written for Server 10.0 and older. There was a 10.1 and newer version produced, but it never gained popularity as you didn't need heavy c#/vb code to interface with the server. Server 10.1+ was released with a well structured REST API, allowing high level languages, such as Python, and thus a wider audience to script and administer it without the need of the heavy agssom.exe utility.
-
But I need to write login and password in my .py script, any idea how to avoid this. For example, can I use .ags file ?)– LoraCommented May 9, 2019 at 19:03
-
You cannot make use of the .ags file as a mechanism to stop/start services. The password needs to be saved in a file, somewhere. Whether thats the .py file itself, or if you want to put it in a separate file that the .py file reads.– KHibmaCommented May 10, 2019 at 11:55
At ArcGIS for Server 10.1+ you can use the ArcGIS Server Administration Toolkit - 10.1+
From the description (bolding is mine):
The ArcGIS Server Administration Toolkit provides you the tools and scripts to help you administer your ArcGIS Server. These tools haven been written in Python and connect to the REST Admin of an ArcGIS Server allowing you to quickly and easily perform some common administrative functions, including: - List item
- Stopping, starting or deleting a service
- Renaming a service
- Modifying the log level and clearing old logs
- Upload and registering an SOE
- Creating a featureclass from the locations a map service was viewed at (by inspecting the logs)
- Publishing Service Definitions (.SD)
The tools inside this kit are presented in three main ways: Tools, Code, and standalone executable.
.ags
file to a token. You might be able to try connecting with a certificate (PEM file), though this requires more than plain vanilla HTTP.