Sort of always wondered how to start a server with as less input as possible. Using WLST we can obtain a lot of information about what is configured in the WebLogic environment, but for that we need to have the admin server running (this is the ‘as less input as possible’ caveat). As such we devide the scripts into three (modular) parts. First, we set-up scripts to start and stop the node manager. Secondly, we set-up scripts to start and stop the admin server. Finally, we set-up scripts to start and stop managed servers in the environment.

NodeManager

As already mentioned in the introduction, we need some input for our scripts. We put these necessities into a properties file

domain_name=base_domain
domain_home=/home/weblogic/weblogic12.1.1/configuration/domains/base_domain
node_manager_username=nodemanager
node_manager_password=magic12c
node_manager_home=/home/weblogic/weblogic12.1.1/configuration/nodemanagers/base_domain
node_manager_listen_port=5556
node_manager_mode=ssl
admin_server_name=AdminServer
admin_username=weblogic
admin_password=magic12c
admin_server_listen_port=7001

Note that the information needed is specific for the admin server and the node manager on the machine where the admin server is running. To start the node manager we can use the following WLST script

import socket;
# socket.gethostbyaddr(socket.gethostbyname(socket.gethostname()))[0] or socket.getfqdn() for short obtains the fully qualified domain name
# more information on using socket can be found in the following link: http://docs.python.org/2/library/socket.html
node_manager_listen_address = socket.gethostname();

startNodeManager(verbose='true', NodeManagerHome=node_manager_home, ListenPort=node_manager_listen_port, ListenAddress=node_manager_listen_address);

The bash script that calls the WLST script looks as follows

#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/startNodeManager.py

Note that in the script above it is assumed that the WLST and bash scripts are present in the same directory. The SetEnvironment.sh sets the path of the weblogic server installation directory (and the JAVA_VENDOR and JAVA_HOME that is used by the commEnv.sh script that is called by wlst.sh), i.e.,

#!/bin/sh

WL_HOME="/home/weblogic/weblogic12.1.1/installation/wlserver_12.1"
export WL_HOME

BEA_JAVA_HOME="/home/weblogic/jrockit-jdk1.6.0_29-R28.2.2-4.1.0"
export BEA_JAVA_HOME

SUN_JAVA_HOME="/home/weblogic/jdk1.7.0_11"
export SUN_JAVA_HOME

JAVA_VENDOR="BEA"
export JAVA_VENDOR

if [ "${JAVA_VENDOR}" = "BEA" ] ; then
	JAVA_HOME="${BEA_JAVA_HOME}"
	export JAVA_HOME
fi

if [ "${JAVA_VENDOR}" = "SUN" ] ; then
	JAVA_HOME="${SUN_JAVA_HOME}"
	export JAVA_HOME
fi

To stop the node manager we can use the following scripts

import socket;
node_manager_listen_address = socket.gethostname();

nmConnect(node_manager_username, node_manager_password, node_manager_listen_address, node_manager_listen_port, domain_name, domain_home, node_manager_mode);
stopNodeManager();
#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/stopNodeManager.py

In general, it is recommended to start the node manager when the machine boots. In this case, we need to know where to put our custom commands that will be called when the system boots. Note that Linux has a /etc/rc.d/rc.local file, that can be used to put custom commands in. Let us do it in the recommended script-based way. Note that Unix-based systems specify so-called run levels, and that for each run level, scripts can be defined that start a certain service. These scripts are located in the /etc/rc.d/init.d directory. This allows for services to be started when the system boots or to be stopped on system shutdown. The different run levels are specified by a specific directory in the /etc/rc.d directory, i.e.,

  • rc0.d – contains scripts that are executed on system shutdown.
  • rc1.d – contains scripts for single-user mode.
  • rc2.d – contains scripts for multi-user mode.
  • rc3.d – contains scripts for multi-user mode and networking.
  • rc4.d – not used.
  • rc5.d – same as rc3.d plus some graphical stuff.
  • rc6.d – contains scripts that are executed on system reboot.

The boot sequence is as follows: in the /etc/inittab file the starting runlevel is defined, the script /etc/rc.d/rc.sysinit is called and /etc/rc.d/rc is run. The rc script looks in the /etc/rc.d/rc<start-runlevel>.d to execute the K**<script-name> scripts with the stop option. After this the S**<script-name> scripts are executed with the start option. Note that scripts are started in numerical order, i.e., the S10network script is executed before the S80sendmail script.

To create a node manager ‘service’, we can use the following script

#!/bin/sh
#
# chkconfig: 235 91 35
# description: starts and stops the node manager
#
#
. /etc/rc.d/init.d/functions

RETVAL=0
SERVICE="nodemanager"

start() {
	echo "Starting Node Manager"
	su - weblogic -c "/home/weblogic/weblogic12.1.1/scripts/NodeManagerStartService.sh" >/dev/null 2>&1
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success || failure
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/${SERVICE}
	return $RETVAL
} 

stop() {
	echo "Stopping Node Manager"
	su - weblogic -c "/home/weblogic/weblogic12.1.1/scripts/NodeManagerStopService.sh" >/dev/null 2>&1
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success || failure
	echo
	[ $RETVAL -eq 0 ] && rm -r /var/lock/subsys/${SERVICE}
	return $RETVAL
} 

restart() {
	stop
	start
} 

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	*)
		echo $"Usage: $0 {start|stop|restart}"
		exit 1
esac 

exit $?

Place this script in the /etc/rc.d/init.d directory. By using the chkconfig command we can update the runlevel information for system services, for example, chkconfig --add nodemanager. To test the set-up shut the system down and start it again. To check if the nodemanager is running we can use either ps -ef|grep java or netstat -anp|grep :5556 which assumes the node manager is listening on port 5556.

Admin Server

To start the admin server we can use the following WLST script

import socket;
node_manager_listen_address = socket.gethostname();

print 'CONNECT TO NODE MANAGER';
nmConnect(node_manager_username, node_manager_password, node_manager_listen_address, node_manager_listen_port, domain_name, domain_home, node_manager_mode);

print 'START ADMIN SERVER';
nmStart(admin_server_name);

print 'DISCONNECT FROM NODE MANAGER';
nmDisconnect();

and the following bash script

#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/startAdminServer.py

To stop the admin server we can use the following scripts

import socket;
node_manager_listen_address = socket.gethostname();
admin_server_listen_address = node_manager_listen_address;
admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port;

print 'CONNECT TO ADMIN SERVER';
connect(admin_username, admin_password, admin_server_url);

print 'CONNECT TO NODE MANAGER';
nmConnect(node_manager_username, node_manager_password, node_manager_listen_address, node_manager_listen_port, domain_name, domain_home, node_manager_mode);

print 'STOPPING ADMIN SERVER';
shutdown(admin_server_name,'Server','true',1000,'true');

print 'DISCONNECT FROM NODE MANAGER';
nmDisconnect();
#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/stopAdminServer.py

Managed Servers

To start managed servers in an environment we can use the following WLST script

import socket;
admin_server_listen_address = socket.gethostname();
admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port;

print 'CONNECT TO ADMIN SERVER';
connect(admin_username, admin_password, admin_server_url);

all_servers = cmo.getServers();
servers = [];
for server in all_servers:
	if (server.getName() != 'AdminServer' and server.getCluster() is not None):
		servers.append(server);

machines = cmo.getMachines();
for machine in machines:
	node_manager_listen_address = machine.getNodeManager().getListenAddress();
	node_manager_listen_port = machine.getNodeManager().getListenPort();
	print 'CONNECT TO NODE MANAGER ON ' + node_manager_listen_address + ':' + repr(node_manager_listen_port);
	nmConnect(node_manager_username, node_manager_password, node_manager_listen_address, node_manager_listen_port, domain_name, domain_home, 'ssl');
	for server in servers:
		if (node_manager_listen_address == server.getListenAddress()):
			print 'STARTING SERVER ' + server.getName();
			start(server.getName(),'Server');
	print 'DISCONNECT FROM NODE MANAGER ON ' + node_manager_listen_address + ':' + repr(node_manager_listen_port);
	nmDisconnect();

print 'DISCONNECT FROM THE ADMIN SERVER';
disconnect();

and the following bash script

#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/startDomain.py

To stop the managed servers we can use the following scripts

import socket;
admin_server_listen_address = socket.gethostname();
admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port;

print 'CONNECT TO ADMIN SERVER';
connect(admin_username, admin_password, admin_server_url);

all_servers = cmo.getServers();
servers = [];
for server in all_servers:
	if (server.getName() != 'AdminServer' and server.getCluster() is not None):
		servers.append(server);

machines = cmo.getMachines();
for machine in machines:
	node_manager_listen_address = machine.getNodeManager().getListenAddress();
	node_manager_listen_port = machine.getNodeManager().getListenPort();
	print 'CONNECT TO NODE MANAGER ON ' + node_manager_listen_address + ':' + repr(node_manager_listen_port);
	nmConnect(node_manager_username, node_manager_password, node_manager_listen_address, node_manager_listen_port, domain_name, domain_home, 'ssl');
	for server in servers:
		if (node_manager_listen_address == server.getListenAddress()):
			print 'SHUTDOWN SERVER ' + server.getName();
			shutdown(server.getName(),'Server','true',1000,'true');
	print 'DISCONNECT FROM NODE MANAGER ON ' + node_manager_listen_address + ':' + repr(node_manager_listen_port);
	nmDisconnect();

print 'DISCONNECT FROM THE ADMIN SERVER';
disconnect();
#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/stopDomain.py

Using Server Life Cycle Runtimes

We can also use the ServerLifeCycleRuntimeMBean to start and stop managed servers. The ServerLifeCycleRuntimeMBean Provides methods that transition servers from one state to another. This class is instantiated only on the Admin Server, and can be used to transition the states of Managed Servers. It cannot be used to start an Admin Server. If it is used to start Managed Servers, we must first set up a Node Manager on each Managed Server’s host machine. In order to start managed servers we can use the following script:

import socket;
admin_server_listen_address = socket.gethostname();
admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port;

print 'CONNECT TO ADMIN SERVER';
connect(admin_username, admin_password, admin_server_url);

print 'START MANAGED SERVERS';
domainRuntime();
server_lifecycles = cmo.getServerLifeCycleRuntimes();

for server_lifecycle in server_lifecycles:
	if (server_lifecycle.getState() != 'RUNNING' and server_lifecycle.getName() != admin_server_name):
		print 'START SERVER ' + server_lifecycle.getName();
		task = server_lifecycle.start();
		while (task.isRunning() == 1):
			print 'STARTING SERVER ' + server_lifecycle.getName();
			java.lang.Thread.sleep(2000);
		print 'SERVER ' + server_lifecycle.getName() + ' is in ' + server_lifecycle.getState() + ' state';
	else:
		print 'SERVER ' + server_lifecycle.getName() + ' is already in ' + server_lifecycle.getState() + ' state and will not be started';

print 'DISCONNECT FROM THE ADMIN SERVER';
disconnect();

and the following bash script

#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/startWebLogicServers.py

When this script is run the following output is observed

[weblogic@middleware-magic scripts]$ ./DomainStartService.sh 

CLASSPATH=/home/weblogic/weblogic12.1.1/installation/patch_wls1211/profiles/default/sys_manifest_classpath/weblogic_patch.jar:/home/weblogic/weblogic12.1.1/installation/patch_ocp371/profiles/default/sys_manifest_classpath/weblogic_patch.jar:/home/weblogic/jrockit-jdk1.6.0_29-R28.2.2-4.1.0/lib/tools.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/weblogic_sp.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/weblogic.jar:/home/weblogic/weblogic12.1.1/installation/modules/features/weblogic.server.modules_12.1.1.0.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/webservices.jar:/home/weblogic/weblogic12.1.1/installation/modules/org.apache.ant_1.7.1/lib/ant-all.jar:/home/weblogic/weblogic12.1.1/installation/modules/net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar::/home/weblogic/weblogic12.1.1/installation/utils/config/10.3/config-launch.jar::/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbynet.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbyclient.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbytools.jar::

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

CONNECT TO ADMIN SERVER
Connecting to t3://middleware-magic.com:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'base_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to domainRuntime tree. This is a read-only tree with DomainMBean as the root.
For more help, use help(domainRuntime)

START SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
STARTING SERVER security-server
SERVER security-server is in RUNNING state
SERVER AdminServer is already in RUNNING state and will not be started
START SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
STARTING SERVER cluster-server1
SERVER cluster-server1 is in RUNNING state
START SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
STARTING SERVER cluster-server2
SERVER cluster-server2 is in RUNNING state
DISCONNECT FROM THE ADMIN SERVER
Disconnected from weblogic server: AdminServer

To stop the managed servers we can use

import socket;
admin_server_listen_address = socket.gethostname();
admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port;

print 'CONNECT TO ADMIN SERVER';
connect(admin_username, admin_password, admin_server_url);

print 'STOP MANAGED SERVERS';
domainRuntime();
server_lifecycles = cmo.getServerLifeCycleRuntimes();

for server_lifecycle in server_lifecycles:
	if (server_lifecycle.getState() == 'RUNNING' and server_lifecycle.getName() != admin_server_name):
		print 'STOP SERVER ' + server_lifecycle.getName();
		task = server_lifecycle.shutdown(1000, java.lang.Boolean('true'));
		while (task.isRunning() == 1):
			print 'STOPPING SERVER ' + server_lifecycle.getName();
			java.lang.Thread.sleep(2000);
                print 'SERVER ' + server_lifecycle.getName() + ' is in ' + server_lifecycle.getState() + ' state';
	else:
		print 'SERVER ' + server_lifecycle.getName() + ' is in ' + server_lifecycle.getState() + ' state and will not be stopped';

print 'DISCONNECT FROM THE ADMIN SERVER';
disconnect();

with the following bash script

#!/bin/sh

# SCRIPT_PATH=$(dirname $0)
SCRIPT=$(readlink -f $0)
SCRIPT_PATH=$(dirname $SCRIPT)

. ${SCRIPT_PATH}/SetEnvironment.sh

${WL_HOME}/common/bin/wlst.sh -loadProperties ${SCRIPT_PATH}/environment.properties ${SCRIPT_PATH}/stopWebLogicServers.py

When this script is run the following output is observed

[weblogic@middleware-magic scripts]$ ./DomainStopService.sh 

CLASSPATH=/home/weblogic/weblogic12.1.1/installation/patch_wls1211/profiles/default/sys_manifest_classpath/weblogic_patch.jar:/home/weblogic/weblogic12.1.1/installation/patch_ocp371/profiles/default/sys_manifest_classpath/weblogic_patch.jar:/home/weblogic/jrockit-jdk1.6.0_29-R28.2.2-4.1.0/lib/tools.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/weblogic_sp.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/weblogic.jar:/home/weblogic/weblogic12.1.1/installation/modules/features/weblogic.server.modules_12.1.1.0.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/server/lib/webservices.jar:/home/weblogic/weblogic12.1.1/installation/modules/org.apache.ant_1.7.1/lib/ant-all.jar:/home/weblogic/weblogic12.1.1/installation/modules/net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar::/home/weblogic/weblogic12.1.1/installation/utils/config/10.3/config-launch.jar::/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbynet.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbyclient.jar:/home/weblogic/weblogic12.1.1/installation/wlserver_12.1/common/derby/lib/derbytools.jar::

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

CONNECT TO ADMIN SERVER
Connecting to t3://middleware-magic.com:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'base_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to domainRuntime tree. This is a read-only tree with DomainMBean as the root.
For more help, use help(domainRuntime)

STOP SERVER security-server
STOPPING SERVER security-server
SERVER security-server is in SHUTDOWN state
SERVER AdminServer is in RUNNING state and will not be stopped
STOP SERVER cluster-server1
STOPPING SERVER cluster-server1
STOPPING SERVER cluster-server1
STOPPING SERVER cluster-server1
SERVER cluster-server1 is in SHUTDOWN state
STOP SERVER cluster-server2
STOPPING SERVER cluster-server2
STOPPING SERVER cluster-server2
STOPPING SERVER cluster-server2
SERVER cluster-server2 is in SHUTDOWN state
DISCONNECT FROM THE ADMIN SERVER
Disconnected from weblogic server: AdminServer

References

[1] Managing the Server Life Cycle.
[2] WebLogic Scripting Tool Command Reference.