Stopping and Starting Services in Portal via Python

3157
4
01-08-2020 08:58 AM
Brian_McLeer
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("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

Brian
4 Replies
JoshuaBixby
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
Arne_Gelfert
Occasional Contributor III

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'}
MichaelVolz
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?

Jen_Zumbado-Hannibal
Occasional Contributor

Thanks @Arne_Gelfert  this helped me more than: https://support.esri.com/en/technical-article/000019994

 

Jen Zumbado-Hannibal, GISP
GIS Coordinator
City of Forest Grove
Forest Grove, OR 97116

0 Kudos