Startup or Shutdown of multiple WebLogic Managed Servers via WLST script

Normally you will have multiple Managed Servers within one WebLogic Server Domain. The classical approach to startup or shutdown these multiple Managed Servers are:

  • using the WebLogic Server provided scripts startManagedWebLogic.[sh|cmd] and stopManagedWebLogic.[sh|cmd] in your Domain Directory
  • using a WLST script with the command shutdown()
The first option by using the startManagedWebLogic.[sh|cmd] and stopManagedWebLogic.[sh|cmd] requires that you provide the name of the Managed Server and the AdminURL, which is not quite flexible.   The second option has a small disadvantage in case you run the command shutdown() against a Managed Server which is currently not running. You will receive error messages like:
 
weblogic.server.ServerLifecycleException: Can not get to the relevant ServerRuntimeMBean for server DemoManaged

Specially when you are trying to shutdown multiple Managed Servers within such a WLST script that uses the command shutdown(), if you hit such an error the WLST script will terminate and the potential following shutdown() commands for other Managed Servers will not be executed.

With below WLST script, you can avoid this problem and perform your startup or shutdown tasks in a more elegant way.

The WLST script is using a properties file, in my case named domain.properties which contains following:

adminurl=t3://localhost:7001
adminusername=weblogic
adminpassword=weblogic
serverlist=DemoManaged,ManagedServer

The property serverlist is containing a comma seperated list of Managed Servers which you want to control.

The WLST script is moreover using an input value (start or stop). Before performing either the start() or the shutdown() command, the script will check for the MBean ServerRuntimeMBean if its existing or not for the given Managed Server.

import getopt
# Load the properties file with all necessary values
loadProperties('domain.properties')
# Split the provided Server List String for the FOR LOOP in start and stop command
serverlistSplit=String(serverlist).split(",")
 
# Get the command, must be 'start' or 'stop'
getcommand=sys.argv[1]

# Check the provided command
if getcommand != 'start' and getcommand != 'stop':
  print 'usage: <start stop="stop">'
  exit()

# Connect to the Weblogic Admin Server
# Connection Details are retrieved from properties file
connect(adminusername, adminpassword, adminurl)

# Change to the root of the MBean hierarchy
domainRuntime()

# Start Block
if getcommand == 'start':
   # Loop over the splitted Server List string
   for s in serverlistSplit:
     # Is a ServerRuntime MBean existing for current Managed Server?
     # If yes, the current Managed Server is already running and we dont need to do anything
     bean = getMBean('ServerRuntimes/' + s)
     if bean:
       print 'Server ' + s + ' is ' + bean.getState()
     else:
       start(s, 'Server', block='false')
       print 'Started Server ' + s

# Stop Block
if getcommand == 'stop':
   # Loop over the splitted Server List string
   for s in serverlistSplit:
     # Is a ServerRuntime MBean existing for current Managed Server?
     # If no, the current Managed Server is already down and we dont need to do anything
     bean = getMBean('ServerRuntimes/' + s)
     if bean:
       shutdown(s, 'Server')
       print 'Stopped Server ' + s
     else:
       print 'Server ' + s + ' is not running'

disconnect()
exit()

Place the properties file domain.properties and the WLST script start_stop_managedservers.py in one directory, adjust the properties file to your landscape.

To call the WLST script start_stop_managedservers.py just source the required script setWLSEnv.[sh|cmd] out of your WLS_HOME/server/bin directory and then execute following command:

java weblogic.WLST start_stop_managedservers.py [start|stop]