Websphere : Jython stop & start server scripts

Posted By Sagar Patil

Stop Script:

Let’s check if there are any java (server) process running ?

[was61@ scripts]$ ps -ef | grep java |  awk ‘{print  $2 ” ”  $8}’
18343 /opt/IBM/WebSphere/AppServer/java/bin/java
18521 /opt/IBM/WebSphere/AppServer/java/bin/java
18639 /opt/IBM/WebSphere/AppServer/java/bin/java
18819 /opt/IBM/WebSphere/AppServer/java/bin/java

Copy this script at your node and use it as “wsadmin.sh -f stopCell.py” (No arguments)

#——————————————————————————-
#  Name: stopCell.py
#  Role: Gracefully terminate all active server processes in the cell
# Usage: wsadmin -f stopCell.py
#    or: ./wsadmin.sh -f stopCell.py
#——————————————————————————-
#  Name: showAsDict.py
#  From: WebSphere Application Server Administration using Jython
#    By: Robert A. (Bob) Gibson [rag], Arthur Kevin McGrath, Noel J. Bergman
#  ISBN: 0-137-00952-6
#——————————————————————————-
def showAsDict( configID ) :
‘Return a dictionary of the AdminConfig.show( configID ) result.’
result = {};
try :
import sys;           # Get access to WSAS objects
#—————————————————————–
# The result of the AdminConfig.show() should be a string
# containing many lines.  Each line of which starts and ends
# with brackets.  The “name” portion should be separated from the
# associated value by a space.
#—————————————————————–
for item in AdminConfig.show( configID ).splitlines() :
if ( item[ 0 ] == ‘[‘ ) and ( item[ -1 ] == ‘]’ ) :
( key, value ) = item[ 1:-1 ].split( ‘ ‘, 1 );
if ( value[ 0 ] == ‘”‘ ) and ( value[ -1 ] == ‘”‘ ) :
value = value[ 1:-1 ];
result[ key ] = value;
except NameError, e :
print ‘Name not found: ‘ + str( e );
except :
( kind, value ) = sys.exc_info()[ :2 ];
print ‘Exception  type: ‘ + str( kind );
print ‘Exception value: ‘ + str( value );
return result;

#———————————————————————
# For each type of server
#———————————————————————
for Type in [ ‘APPLICATION_SERVER’, ‘NODE_AGENT’, ‘DEPLOYMENT_MANAGER’ ] :
#——————————————————————-
# For each configured server
#——————————————————————-
for server in AdminConfig.list( ‘Server’ ).splitlines() :
#—————————————————————–
# If this server is of the current type
#—————————————————————–
sDict = showAsDict( server );
if sDict[ ‘serverType’ ] == Type :
bean = AdminConfig.getObjectName( server );
#—————————————————————
# Is this server active
#—————————————————————
if bean :
#————————————————————-
# stop it (asynchronously)
#————————————————————-
print ‘Stoppping: ‘ + sDict[ ‘name’ ];
AdminControl.invoke( bean, ‘stop’ );

Usage :

[was61@ scripts]$  /opt/IBM/WebSphere/AppServer/bin/wsadmin.sh -f stopcell.py
WASX7209I: Connected to process “dmgr” on node server1_Manager using SOAP connector;  The type of process is: DeploymentManager
Stoppping: server_member1
Stoppping: server_member2
Stoppping: nodeagent
Stoppping: dmgr

Start Script:

Let’s check if there are any java (server) process running ?

[was61@scripts]$ ps -ef | grep java |  awk ‘{print  $2 ” ”  $8}’
19420 grep

#———————————————————————
# Name: startCell()
# Role: Attempt to start all of the Application Servers in the
#       specified WebSphere Application Server cell.
#———————————————————————
”’
Program: %(cmdName)s.py\n
Role: Attempt to start all of the Application Servers in the
specified WebSphere Application Server cell.\n
Usage: python %(cmdName)s.py <WSAS_HOME>\n
Example: python %(cmdName)s.py C:\\IBM\\WebSphere\\AppServer
”’
import os, sys, re;
__Debug__  = True;
__dashes__ = ‘-‘ * 79;

#———————————————————————
# Name: dirCheck()
# Role: Verify that the specified directory exists, and is a directory
#———————————————————————
def dirCheck( path ) :
return os.path.exists( path ) and os.path.isdir( path );

#———————————————————————
# Name: log()
# Role: If debugging is enabled, log the specified message
#———————————————————————
def log( msg ) :
if __Debug__ :
print msg;

#———————————————————————
# Name: getProfiles()
# Role: Determine the names of the existing profiles
#———————————————————————
def getProfiles( binPath, tempFile ) :
log( ‘getProfiles(): Enter’ );
try :
if os.name == ‘nt’ :
cmd =  ‘manageprofiles’;
else :
cmd =  ‘./manageprofiles.sh’;
exe = os.path.join( binPath, cmd );
log( ‘Calling: %s -listProfiles’ % exe );
rc  = os.system( exe + ‘ -listProfiles >’ + tempFile );
fh  = open( tempFile );
result = fh.read();
log( ‘Result:\n%s\n%s%s’ % ( __dashes__, result, __dashes__ ) );
result = result[ 1:-2 ].split( ‘, ‘ );
fh.close();
except :
Type, value = sys.exc_info()[ :2 ];
log( ‘Exception  Type: ‘ + `Type` );
log( ‘          value: ‘ + `value` );
result = [];
log( ‘getProfiles(): Exit  result = ‘ + `result` );
return result;

#———————————————————————
# Name: getStoppedServers()
# Role: Determine the Server name and type for the specified profiles
# Note: We’re only interested in “stopped” servers, we ignore started
#       ones.
#     – The 3 Types of Servers are:
#       Deployment Manager
#       Node Agent
#       Application Server
#———————————————————————
def getStoppedServers( binPath, tempFile, profileNames ) :
log( ‘getStoppedServers(): Enter’ );
result = {};
pat = re.compile( ‘^ADMU0509I: The (\w+ \w+) “(\w+)”‘ )
for pName in profileNames :
try :
if os.name == ‘nt’ :
cmd =  ‘serverStatus’;
else :
cmd =  ‘./serverStatus.sh’;
exe = os.path.join( binPath, cmd );
log( ‘Calling: %s -all -profileName %s’ % ( exe, pName ) );
rc  = os.system( ‘%s -all -profileName %s >%s’ % ( exe, pName, tempFile ) );
fh  = open( tempFile );
log( ‘Result:\n%s’ % __dashes__ );
for line in fh.read().splitlines() :
mo = pat.match( line );
if mo :
Type, sName = mo.group( 1 ), mo.group( 2 );
log( ‘Stopped: %s -profileName %s’ % ( sName, pName ) );
if result.has_key( Type ) :
result[ Type ].append( ( pName, sName ) );
else :
result[ Type ] = [ ( pName, sName ) ];
log( __dashes__ );
fh.close();
except :
value = str( sys.exc_info()[ 1 ] );
print ‘Error processing profileName %s: %s’ % ( pName, value );
log( ‘getStoppedServers(): Exit  result =’ + `result` );
return result;

#———————————————————————
# Name: startCell()
# Role: Start the application servers in the specified cell.
#———————————————————————
def startCell() :
log( ‘startCell(): Enter’ );

#——————————————————————-
# How many user specified command line options exist?
#——————————————————————-
argc = len( sys.argv );
if argc != 2 :
Usage();
else :
try :
#—————————————————————
# pgm = fully qualified path to script being executed
#—————————————————————
pgm  = sys.argv[ 0 ];
if not pgm.endswith( ‘.py’ ) :
raise ValueError( ‘Unrecognized script type: “%s”‘ % pgm );
tempfile = pgm[ :-2 ] + ‘temp’;
#     print ‘tempfile: “%s”‘ % tempfile;

#—————————————————————
# Verify that washome exists, and is a directory
#—————————————————————
WASerror = ‘”%s” does not appear to be a WAS Home directory.’;
washome = sys.argv[ 1 ];
if not dirCheck( washome ) :
print WASerror % washome;
sys.exit();

wasbin  = os.path.join( washome, ‘bin’ );
if not dirCheck( wasbin ) :
print WASerror % wasbin;
sys.exit();

#—————————————————————
# Get a list of the available profileNames
#—————————————————————
profileNames = getProfiles( wasbin, tempfile );

#—————————————————————
# For each Server Type, list the profilName and serverName of
# each stopped server
#—————————————————————
stopped = getStoppedServers( wasbin, tempfile, profileNames );

#—————————————————————
# width = length of longest server name
#—————————————————————
sNames = [];
for Type in stopped :
for n, s in stopped[ Type ] :
sNames.append( s );
width = max( [ len( x ) for x in sNames ] );

#—————————————————————
# Process each Type, in the right order to start the cell
#—————————————————————
for Type in [ ‘Deployment Manager’, ‘Node Agent’, ‘Application Server’ ] :
if stopped.has_key( Type ) :
for pName, sName in stopped[ Type ] :
startServer( wasbin, pName, sName, tempfile, -width );

#—————————————————————
# Remove the temporary file
#—————————————————————
try :
os.unlink( tempfile );
except :
pass;
except :
Type, value = sys.exc_info()[ :2 ];
Type, value = str( Type ), str( value );
print ‘Exception type: %s\n         value: %s’ % ( Type, value );
log( ‘startCell(): Exit’ );

#———————————————————————
# Name: startServer()
# Role: Execute the WebSphere Application Server “startServer” command
#———————————————————————
def startServer( binPath, profile, server, tempFile, width = None ) :
log( ‘startServer(): Enter’ );
try :
if os.name == ‘nt’ :
cmd =  ‘startServer’;
else :
cmd =  ‘./startServer.sh’;
log( ‘Calling: %s %s -profileName %s’ % ( cmd, server, profile ) );
exe = os.path.join( binPath, cmd );
if width :
print ‘%s %*s -profileName %s’ % ( cmd, width, server, profile ),
else :
print ‘%s %s -profileName %s’ % ( cmd, server, profile ),
cmd = ‘%s %s -profileName %s >%s’ % ( exe, server, profile, tempFile );
rc  = os.system( cmd );
fh  = open( tempFile )
tail = fh.read().splitlines()[ -1 ];
fh.close();
if tail.startswith( ‘ADMU3000I:’ ) :
log( ‘  Started successfully.’ );
result = 0;
if not __Debug__ :
print;
else :
log( ‘  Start failed:’, tail );
print ‘  Error – tail = “%s”‘ % tail;
result = 1;
except :
value = str( sys.exc_info()[ 2 ] );
print ‘  Error – ‘, value;
result = -1;
log( ‘startServer(): Exit’ );
return result;

#———————————————————————
# Name: Usage()
# Role: Descript script function
#———————————————————————
def Usage( cmdName = ‘startCell’ ) :
print __doc__ % locals();
sys.exit( -1 );

#———————————————————————
# main entry point – verify that script was executed, not imported
#———————————————————————
if __name__ == ‘__main__’ :
if os.name in [ ‘nt’, ‘posix’ ] :
startCell();
else :
print ‘Unsupported Operating System type: %s\n’ % os.name;
Usage();
else :
Usage( __name__ );

Usage (Ignore)

[was61@scripts]$ /opt/IBM/WebSphere/AppServer/bin/wsadmin.sh -f startcell.py
WASX7023E: Error creating “SOAP” connection to host “localhost”; exception information: com.ibm.websphere.management.exception.ConnectorNotAvailableException: com.ibm.websphere.management.exception.ConnectorNotAvailableException: ADMC0016E: The system cannot create a SOAP connector to connect to host localhost at port 8879.
WASX7213I: This scripting client is not connected to a server process; please refer to the log file /opt/IBM/WebSphere/AppServer/profiles/Profile01/dmgr/logs/wsadmin.traceout for additional information.
WASX8011W: AdminTask object is not available.
Program: main.py
Role: Attempt to start all of the Application Servers in the
specified WebSphere Application Server cell.
Usage: python main.py <WSAS_HOME>
Example: python main.py C:\IBM\WebSphere\AppServer

(Please note you will need python installed on your box to run this script)

[was61@scripts]$ python startcell.py /opt/IBM/WebSphere/AppServer

startServer dmgr -profileName dmgr

I have tested it and it is only bringing up DMGR,Will need some tweaking

[was61@scripts]$ ps -ef | grep java |  awk ‘{print  $2 ” ”  $8}’
19716 /opt/IBM/WebSphere/AppServer/java/bin/java

Leave a Reply

You must be logged in to post a comment.

Top of Page

Top menu