Stopping and Starting Services in Portal via Python

288
2
01-08-2020 08:58 AM
Highlighted
Occasional Contributor

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("https://url.to.com/portal", "Username", "Password", verify_cert=False)
services = gis_servers.services.list()
#To stop specific service(s)
for service in services:
for service in server.services.list():
if service.properties.serviceName == "SampleWorldCities":
service.stop()

Error Message: 

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

2 Replies
Highlighted
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.

Reply
0 Kudos
Highlighted
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_server.services.list()
one_of_those_services = services_on_that_server[0]

# provided your service is running to begin with
print(one_of_those_services.status)
one_of_those_services.stop()
print(one_of_those_services.status)
one_of_those_services.start()
print(one_of_those_services.status)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This gives me the following output:

{'configuredState': 'STARTED', 'realTimeState': 'STARTED'}
{'configuredState': 'STOPPED', 'realTimeState': 'STOPPED'}
{'configuredState': 'STARTED', 'realTimeState': 'STARTED'}
Reply
0 Kudos