Using Scripts to Manage Server Lifecycle

Important

If a domain does not use Node Manager to start and stop servers, you can use the default server start/stop scripts from WebLogic or use a wrapper script to invoke WebLogic Scripting Tool (WLST).

Best practices for using custom scripts

Do not use custom scripts unless necessary. However, if you have to use custom scripts to manage server control lifecycle, review the following the recommended practices.

  • Do not read input from stdin or script parameters. WebLogic Management sends the server name for managed server scripts but nothing is sent for administration server.
  • Handle error conditions at each step. This allows WebLogic Management to handle the failures efficiently.
  • Do not invoke scripts like startWebLogic.sh without a proper exit criteria as calling such script will not exit the caller. The startWebLogic.sh script starts the server and does not return back until the server process is dead. This causes WebLogic Management plugin to hang waiting for response. WebLogic Management avoids waiting by invoking the script, but it is always a best practice to exit a custom script gracefully.
  • If the domain uses Node Manager, use WLST to perform the operation and invoke a WLST script from shell script (Linux) or batch file (Windows).
  • If the administration server takes longer than 5 minutes, adjust the properties in /usr/libexec/oracle-cloud-agent/plugins/oci-wlms/_internal/wls_actions/wlst/properties.json to provide values that would be optimal for the environment. For example:
    "wls_max_retries": 10, (The number of times to poll for the admin server to check if the admin port is opened)
    "wls_sleep_interval": 30 (The polling interval)

Using default server start/stop scripts from WebLogic

Use the startWebLogic.sh/cmd and stopWebLogic.sh/cmd for the administration server and startManagedWebLogic.sh/cmd and stopManagedWebLogic.sh/cmd for managed servers. Configure the domain settings in WebLogic Management to use these scripts. See Configuring Patching and Server Control.

Once configured, WebLogic Management invokes the stopWebLogic.sh without parameters and stop/start ManagedWebLogic.sh scripts by passing the server name as a parameter.

When using these scripts, WebLogic Management does not monitor or poll the status of starting managed servers or stopping any servers. This can lead to a lifecycle operation reporting success even though the servers were not stopped or started successfully.

Using wrapper script to invoke WLST

Use WLST to connect to the administration server and then use the use administration server connection to manage servers. The WLST script is invoked by a wrapper shell script which is used in the start and stop script setting in WebLogic Management. The following are the sample wrapper scripts.

Stop administration server:

# stop Admin script

import sys
 
wls_user = <weblogic-admin-user-name>
wls_password = <weblogic-admin-password>
admin_url = <admin_url>
try:
    connect(wls_user, wls_password, admin_url)
except Exception, e:
    # Handle exception here and identify if the server is running
    if "Error getting the initial context. There is no server running at" in str(e):
        # This error means, there is no server running. So nothing to stop
        sys.exit(0)
    else:
        sys.exit(1)
 
# We are connected. now shutdown the server
try:
    shutdown()
    sys.exit(0)
except:
    # Failed to shutdown. Exit with non-zero
    sys.exit(3)

Shell script to invoke the stop admin operation:

#!/bin/bash

# $MW_HOME - The path to the MW_HOME
$MW_HOME/oracle_common/common/bin/wlst.sh <stop-admin-python script>
Important

For starting the Administration server, provide the startWebLogic.sh script as WebLogic Management polls for the port to determine if the administration server is started successfully.

Start managed server:

# start managed server

import sys
 
managed_server_name = sys.argv[1]
wls_user = <weblogic-admin-user-name>
wls_password = <weblogic-admin-password>
admin_url = <admin_url>
 
# connect to the admin server and then start the managed server
 
try:
    connect(wls_user, wls_password, admin_url)
except Exception, e:
    # We cannot connect to admin and hence we cannot start the managed server
    sys.exit(1)     
 
# We are connected. now start the server
try:
    start(managed_server_name)
    sys.exit(0)
except:
    # Failed to start server. Exit with non-zero
    sys.exit(2)

Script to invoke managed server start/stop operation:

#!/bin/bash

# $MW_HOME - The path to the MW_HOME
SERVER_NAME=$1
$MW_HOME/oracle_common/common/bin/wlst.sh <stop/start-managedserver-python script> $SERVER_NAME