Automating Start/Stop AGS and AGS services
I was working on setting up our automated build process for one of the projects I am working on and needed a way to start/stop the ArcGIS server running on a remote machine and also start/stop specific ArcGIS services running on remote machines. I was looking to see if ArcGIS Server provided any command line tools to perform these tasks so that I can just call these commands from my build file. But there is no out-of-the-box command line tools that come with ArcGIS Server which I could use for that purpose. So, I had to improvise and use some utilities and cook up a little bit of code.
To start/stop ArcGIS Services running on a remote machine, I was able to use the “PsService” from the Sysnternals Process utilities. Here is the crux of what PsService does.
PsService displays the status, configuration, and dependencies of a service, and allows you to start, stop, pause, resume and restart them. Unlike the SC utility, PsService enables you to logon to a remote system using a different account, for cases when the account from which you run it doesn’t have required permissions on the remote system
Usage: psservice [\\computer [-u username] [-p password]] <command> <options>
So, we can use the “stop” command and provide the service name for ArcGIS Server process “ArcServerObjectManager” as the option. Here is an example showing how it can be used.
psservice \\MachineName -u username -p password stop ArcServerObjectManager
Since, I had to call it from my NAnt build file, here is a sample of how it can be done
Setting up NAnt build file properties
<property name =“AGSMachineName” value=“myMachine”/>
<property name =“AGSAdminUser” value=“myUserName”/>
<property name =“AGSAdminPassword” value=“myPassword”/>
<!–comma seperated list of MapServices that needs to started/stopped–>
<property name =“AGSMapServices” value=“BaseMap, Boundaries, DynamicMap”/>
Targets to Start and Stop ArcGIS Server
<target name =“StopAGS”>
<exec program=“psservice” basedir=“.\tools\pstools\” append=“true”>
<arg line=“\\${AGSMachineName} -u ${AGSAdminUser} -p ${AGSAdminPassword} stop ArcServerObjectManager”/>
</exec>
</target>
<target name =“StartAGS”>
<exec program=“psservice” basedir=“.\tools\pstools\” append=“true”>
<arg line=“\\${AGSMachineName} -u ${AGSAdminUser} -p ${AGSAdminPassword} start ArcServerObjectManager”/>
</exec>
</target>
Now that’s good. The first hurdle has been crossed. But I still needed to start and stop specific MapServices that I desired. I couldn’t shortcut my way through this one and had to write some code to do this. The code to do it was pretty simple and didn’t turn out to be a pain.
The tool to start/stop ArcGIS services can be downloaded here
The source code for this tool can be downloaded here
The syntax to use the tool from the command line is
Usage: arcgisservice [serverName] [serviceName] [serviceType] [start | stop | restart] <username> <password>
The “username” & “password” for a user belonging to the “agsadmin” group can be specified here. If they are not specified, your current windows credentials will be used. And here is how it can be used in the NAnt build file.
Targets to Start and Stop ArcGIS MapServices specified as a comma seperated list
<target name=“StopMaps”>
<foreach item=“String” in=“${AGSMapServices}” delim=“,” property=“mapService”>
<exec program=“agsUtil” basedir=“.\tools\AGSTools\” append=“true”>
<arg line=” ${AGSMachineName} ${mapService} MapServer stop ${AGSAdminUser} ${AGSAdminPassword}”/>
</exec>
</foreach>
</target>
<target name=“StartMaps”>
<foreach item=“String” in=“${AGSMapServices}” delim=“,” property=“mapService”>
<exec program=“agsUtil” basedir=“.\tools\AGSTools\” append=“true”>
<arg line=“${AGSMachineName} ${mapService} MapServer start ${AGSAdminUser} ${AGSAdminPassword}”/>
</exec>
</foreach>
</target>
The target above will loop through and stop/start all the MapServices specified as a comma separated list in the “AGSMapServices” NAnt property.
And if you want to do the above and much more through another GUI didn’t want ArcCatalog installed all over the place, then the SOEXplorer would be the way to go.
You can get the same modifiying this Windows Scripting Host:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colServiceList = objWMIService.ExecQuery _
(“Select * from Win32_Service where Name = ‘Alerter'”)
For Each objService in colServiceList
If objService.State = “Running” Then
objService.StopService()
Wscript.Sleep 5000
End If
errReturnCode = objService.ChangeStartMode(“Disabled”)
Next
Or using the new PowerShell command line Star/Stop-service
you can also use ESRI.ArcGIS.Server.IGISServerConnection2 to connect and change status of services.
The following function help modify service status by connecting to remote computer.
So you can use it to stop som or soc by passing its service “name”.
Public Function Set_ServiceStatus(ByVal strUsername As String, ByVal strPassword As String, ByVal strDomain As String, ByVal strServiceName As String, ByVal bStart As Boolean) As Boolean
Dim bresult As Boolean = False
Try
Dim myConnectionOptions As New System.Management.ConnectionOptions
Dim query_command As String = “”
Dim select_query As Management.SelectQuery = Nothing
Dim myManagementscope As System.Management.ManagementScope = Nothing
Dim ManObjSearch As System.Management.ManagementObjectSearcher = Nothing
Dim pQueryColl As Management.ManagementObjectCollection = Nothing
Dim args As Object = Nothing
Dim pObjectEnumerator As Management.ManagementObjectCollection.ManagementObjectEnumerator = Nothing
With myConnectionOptions
.Username = strUsername
.Password = strPassword
.Impersonation = Management.ImpersonationLevel.Impersonate
End With
myManagementscope = New System.Management.ManagementScope(“\\” & strDomain & “\root\cimv2”, myConnectionOptions)
myManagementscope.Connect()
If myManagementscope.IsConnected = True Then
query_command = “SELECT * FROM Win32_Service Where Name = ” & “‘” & strServiceName & “‘” ‘(here service name can be “‘ArcServerObjectManager'”)
select_query = New Management.SelectQuery(query_command)
ManObjSearch = New System.Management.ManagementObjectSearcher(myManagementscope, select_query)
pQueryColl = ManObjSearch.Get()
pObjectEnumerator = pQueryColl.GetEnumerator
While pObjectEnumerator.MoveNext()
Dim pObject As Management.ManagementObject = CType(pObjectEnumerator.Current, Management.ManagementObject)
Debug.Print(pObject(“DisplayName”))
If bStart = True Then
If LCase(pObject(“Started”)) LCase(“True”) Then
pObject.InvokeMethod(“StartService”, args)
End If
bresult = True
Else
If LCase(pObject(“Started”)) LCase(“False”) Then
pObject.InvokeMethod(“StopService”, args)
End If
bresult = True
End If
End While
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return bresult
End Function
The download you have seems to be for ArcGIS 9.3 – do you have a version for ArcGIS 10 ?
You can use the following script:
http://arcscripts.esri.com/details.asp?dbid=16293
It is also compiled for ArcGIS 10.
I found the link on: http://gis.utah.gov/code-c-/arcgis-server-batch-stop-and-start-services
Is there any way to be notify when one of the map services is down?
We use Nagios to monitor map services.