Stopping and Starting Services in Portal via Python

01-08-2020 08:58 AM
Occasional Contributor II

Trying to start/stop specific services. Using the article at How To: Stop GIS services using ArcGIS API for Python, I tried using the code but keep receiving an error message below posted code. I am running this script in IDLE (ArcGIS Pro). Using just IDLE for ArcGIS Desktop did not have the necessary modules. 

from arcgis.gis import GIS
import arcgis.gis.admin
gis = GIS("", "Username", "Password", verify_cert=False)
services =
#To stop specific service(s)
for service in services:
 for service in
 if == "SampleWorldCities":

Error Message: 

Traceback (most recent call last):
File "Z:\Temp\Brian\", line 5, in <module>
services =
NameError: name 'gis_servers' is not defined

4 Replies
MVP Esteemed Contributor

All your error message is stating is that you are using a variable, gis_servers, that has never been defined in your code.  Look at the "full code" at the bottom on that page.

0 Kudos
Regular Contributor

Joshua is absolutely right. You need to get to the servers first.

If you don't use the Python API a lot, it can be a little confusing. I have to always refer to script I've used before. I had a snipped lying around... to turn on/off services.

from arcgis import gis

# I like this interactive way whenever possible instead of storing passwords
mygis = gis.GIS(<your_portal_path>, username='arcgis_python')

servers = mygis.admin.servers.list()
one_server = servers[0]

services_on_that_server =
one_of_those_services = services_on_that_server[0]

# provided your service is running to begin with

This gives me the following output:

{'configuredState': 'STARTED', 'realTimeState': 'STARTED'}
{'configuredState': 'STOPPED', 'realTimeState': 'STOPPED'}
{'configuredState': 'STARTED', 'realTimeState': 'STARTED'}
Esteemed Contributor

Is the Portal path the portal home URL without the /home at the end?

Does the user specified need to be defined as an Administrator in your portal?

Did you need to clone your python environment and add a specific module to python to access the GIS module?

Occasional Contributor
0 Kudos