Stopping Service via Python

461
9
02-27-2024 05:38 AM
JoshBillings
Occasional Contributor II

Hello all,

I am using the code below to stop a service on ArcGIS Server. This work flawlessly on this particular server. 

When I try to use this code on a service on a different server, I get a KeyError: 'folders' error on line 8. Not sure what this error is indicating.

Does anyone know why this would work on one server but not the other? Thanks!

from arcgis import *
from arcgis.gis import server

s = server.Server("hhtps://server-url.com",username='username',password='password')

cdir = s.content

serv = cdir.get("Service Name",folder = "Folder")

serv.service.stop()

 

0 Kudos
9 Replies
CodyPatterson
Occasional Contributor III

Hey @JoshBillings 

If you were to print s, cidr, and serv, would you get the expected results as the last server?

I assume what's happening is that the location of a folder may have changed or is not present within the directory listed.

Hope that help!

Cody

0 Kudos
JoshBillings
Occasional Contributor II

Hey @CodyPatterson, thanks for the reply! I was able to print s and cdir and it was as expected. I can't get serv printed because it's throwing an error on that line. Is there a way I can force print that even though there is an error?

I also double checked to make sure I had Service Name and Folder name spelled right and that it was actually on the server and it is. 

0 Kudos
CodyPatterson
Occasional Contributor III

Hey @JoshBillings 

Do you happen to have a screenshot of the error that it's throwing? I may be able to look a little deeper into it!

It's good that everything else seems to be in order, we may just be down to one issue now!

Cody

0 Kudos
JoshBillings
Occasional Contributor II

@CodyPatterson- Screenshot is attached. In the screenshot it says "line 20" but that is referencing the serv variable.

 

0 Kudos
MichaelVolz
Esteemed Contributor

Is this running python code from Pro (3.x) or from ArcMap (2.7)?

0 Kudos
JoshBillings
Occasional Contributor II

Hey @MichaelVolz. I'm running this from Pro and it's using Python 3.9.18.

0 Kudos
MichaelVolz
Esteemed Contributor

Hi Josh:

What version of Pro is that python version associated with?

I ask because I had a Pro python script that stopped GIS services that was working in Pro 2.x.  Unfortunately, the upgrade to Pro 3.x broke the script due to the architectural changes of Pro 3.x compared to 2.x with regards to security.  ESRI development has been looking into issue, but I have not heard back from ESRI if the issue has been resolved (It has been like this for 6 months).

Not sure if its the same exact issue, but both your script and my script were designed to stop services.

0 Kudos
JoshBillings
Occasional Contributor II

Michael,

I actually did recently upgrade Pro to 3.2. I was using 3.0.3 before. However, the script above will work for the other server in Pro 3.2, so it seems that may be an unlikely cause. 

I was able to use the code below to stop my services, but this stops all services in a particular folder. I would still like to use my original code to stop specific services, so I'll still be looking into this. 

 

 

from arcgis.gis import GIS

# Set ArcGIS Server or Portal Instance
gis = GIS("https://server-url/portal", "username", "password", verify_cert=False)

# Get server list and return first server
serverList = gis.admin.servers.list()[0]

# Get list of Services in a specific folder
serviceList = serverList.services.list(folder="FolderName")

# Stop Services specified folder
print("Stopping Services...")
for service in serviceList:
     service.stop()
     
print("Services Stopped!")

 

 

0 Kudos
JoshBillings
Occasional Contributor II

One can stop specific services by using indexing on the "serviceList" variable.

from arcgis.gis import GIS

# Set ArcGIS Server or Portal Instance
gis = GIS("https://server-url/portal", "username", "password", verify_cert=False)

# Get server list and return first server
serverList = gis.admin.servers.list()[0]

# Get list of Services in a specific folder
serviceList = serverList.services.list(folder="FolderName")

# Get Specific Service through Index and Stop the Service
specificService = serviceList[0]
specificService.stop()
0 Kudos