JBoss startup using init.d and /etc/j...

JBoss startup using init.d and /etc/jbtab

 
Assumes that all jboss apps live in /apps/jboss/default/server/....   where default is a link to the current version of jboss and all start/stop scripts follow the startJBoss.sh and stopJBoss.sh convention
 
/etc/jbtab:

# JBoss Instance        Username
myapp1       jbadmin
myapp2                     jbadmin
myapp3          jbadmin
 
 
init.d/jboss

#!/bin/sh
#
# JBoss init script
#chkconfig: 2345 97 05
#description: JBoss Application Server
# Source function library.
if [ -f /etc/init.d/functions ] ; then
        . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
else
        exit 0
fi

if [ ! -z "$2" ]; then APP_NAME=$2; fi

start () {
        TABLINE=`grep "^$APP_NAME" /etc/jbtab`

        if [ -z "$TABLINE" ]; then
                echo "Application not found in jbtab: $APP_NAME"
                exit 1
        fi

        AGENT_USER=`echo $TABLINE | awk '{print $2}'`
        APP_HOME=/apps/jboss/default/server/$APP_NAME/bin

        echo -n "Starting $APP_NAME: "

        # start daemon
        su - ${AGENT_USER} -c "cd ${APP_HOME}; ./startJBoss.sh"
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/$APP_NAME
        return $RETVAL
}

stop () {
        TABLINE=`grep "^$APP_NAME" /etc/jbtab`

        if [ -z "$TABLINE" ]; then
                echo "Application not found in jbtab: $APP_NAME"
                exit 1
        fi

        AGENT_USER=`echo $TABLINE | awk '{print $2}'`
        APP_HOME=/apps/jboss/default/server/$APP_NAME/bin

        # stop daemon
        echo -n "Stopping $APP_NAME: "
        su - ${AGENT_USER} -c "cd ${APP_HOME};./stopJBoss.sh"
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/$APP_NAME
        return $RETVAL
}

restart() {
        stop
        start
}

case $1 in
        start)
                if [ -z "$2" ]; then
                        echo "WARNING: Operating on all instances, will proceed in 5 seconds"
                        sleep 5

                        for a in `cat /etc/jbtab | grep -v '^#' | awk '{print $1}'`; do
                                APP_NAME=$a
                                #echo "Would have started $APP_NAME"
                                start
                        done
                else
                        APP_NAME=$2
                        start
                fi
        ;;
        stop)
                if [ -z "$2" ]; then
                        echo "WARNING: Operating on all instances, will proceed in 5 seconds"
                        sleep 5

                        for a in `cat /etc/jbtab | grep -v '^#' | awk '{print $1}'`; do
                                APP_NAME=$a
                                #echo "Would have stopped $APP_NAME"
                                stop
                        done
                else
                        APP_NAME=$2
                        stop
                fi
        ;;
        restart|reload)
                if [ -z "$2" ]; then
                        echo "Restart only operates on one instance at a time."
                        echo "Please specify instance after restart keyword."
                        exit 1
                else
                        APP_NAME=$2
                        restart
                fi
        ;;
        *)

        echo "Usage: $prog {start|stop|restart} <appname>"
        echo "If no appname is specified all apps in /etc/jbtab will be acted upon"
        exit 1
esac

exit $RETVAL

 
 
example startup script startJboss.sh:
 

#!/bin/sh
### ====================================================================== ###
##                                                                          ##
##  JBoss Bootstrap Script                                                  ##
##                                                                          ##
### ====================================================================== ###

echo "Starting JBoss"

PROGNAME=`basename $0`
whoiam=$(/usr/bin/id --user --name)
if [[ "$whoiam" != "jbadmin" ]]
then
    echo "$PROGNAME: This script must be run as jbadmin."
    exit -1
fi

export JAVA_HOME="/apps/java/default"
export JBOSS_HOME="/apps/jboss/default"
export PATH=${JAVA_HOME}/bin:${PATH}
JAVA=${JAVA_HOME}/bin/java

# make the JBOSS_HOME the real path not a slimebolic link.
WD=$(/bin/pwd)
cd ${JBOSS_HOME}
jb=$(/bin/pwd)
export JBOSS_HOME="$jb"
cd $WD

CLASSPATH="${JAVA_HOME}/lib/tools.jar"
export CLASSPATH

# trick to find which server we are running
WD=$(/bin/pwd)

case "${0}" in
    /*)
        cmdDir=$(dirname "$0")
        ;;
    *)
        cmdDir=$(dirname "${WD}/$0")
        ;;
esac

cd  $cmdDir
wrkDir=$(/bin/pwd)
SERVER=$(echo $wrkDir| sed -e "s^${JBOSS_HOME}/server/^^" | sed -e "s^/.*^^")
SERVERDIR="${JBOSS_HOME}/server/${SERVER}"
pid=${SERVERDIR}/${SERVER}.pid
APPDIR="/apps/$SERVER/server"
LOGDIR="/log/$SERVER"
LOGFILE="${LOGDIR}/${SERVER}.log"

#
# Helper to complain.
#
warn() {
        echo "${PROGNAME}: $*"
}

#
# Helper to puke.
#
die() {
        warn $*
        exit 1
}

# The properties file for setting up hostIp, jvmDebugPort, and jmxRemotePort
setup="$APPDIR/${SERVER}/${SERVER}-server.properties"

if [[ -f ${setup} ]]
then
        echo "Reading server setup from ${setup}"
        hostIp=""
        source ${setup}

    if [[ ! $hostIp ]]
    then
        # The host IP must exist on the server properties file.
        die "Mandatory host IP is not found in $setup."
    fi
else
        die "Mandatory setup file (${setup}) does not exist.  Aborting."
fi

# Use the maximum available, or set MAX_FD != -1 to use that
MAX_FD="maximum"

# Increase the maximum file descriptors if we can
# ulimit -n 1024

# Setup the classpath
runjar="$JBOSS_HOME/bin/run.jar"
if [ ! -f "$runjar" ]; then
        die "Missing required file: $runjar"
fi
JBOSS_BOOT_CLASSPATH="$runjar"

# Include the JDK javac compiler for JSP pages. The default is for a Sun JDK
# compatible distribution which JAVA_HOME points to
if [ "x$JAVAC_JAR" = "x" ]; then
        JAVAC_JAR="$JAVA_HOME/lib/tools.jar"
fi

if [ "x$JBOSS_CLASSPATH" = "x" ]; then
        JBOSS_CLASSPATH="$JBOSS_BOOT_CLASSPATH:$JAVAC_JAR"
else
        JBOSS_CLASSPATH="$JBOSS_CLASSPATH:$JBOSS_BOOT_CLASSPATH:$JAVAC_JAR"
fi

CLASSPATH="$JBOSS_CLASSPATH"

if [ "x$jvmDebugPort" = "x" ] ; then
        DEBUG_OPTS=""
else
        DEBUG_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=${jvmDebugPort},server=y,suspend=n"
fi

if [ "x$jmxRemotePort" = "x" ] ; then
        JMX_OPTS=""
else
        JMX_OPTS="-Dcom.sun.management.jmxremote"
        JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.port=${jmxRemotePort}"
        JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
        JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
fi

# don't use JMX or DEBUG on this server
DEBUG_OPTS=""
JMX_OPTS=""

LOG_OPTS="-Dlog4j.debug=true"

# set MaxPermSize to avoid  "java.lang.OutOfMemoryError: PermGen" space error
# gcInterval time is set to avoid excessive CPU usage for garbage collection
# recommendations are between ten minutes (600000) & one hour (3600000)
# see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6200091
#
JAVA_MEM="-Xms128m -Xmx512m -XX:MaxPermSize=64m -Dsun.rmi.dgc.client.gcInterval=600000 -Dsun.rmi.dgc.server.gcInterval=600000"
SERVER_OPTS="-server"
JAVA_OPTS="${SERVER_OPTS} ${JAVA_MEM} ${DEBUG_OPTS} ${JMX_OPTS} ${JAVA_OPTS}"
# sets up LDAP pools
# add this if you want to debug the pools
# -Dcom.sun.jndi.ldap.connect.pool.debug=fine
#LDAP="-Dcom.sun.jndi.ldap.connect.pool=true -Dcom.sun.jndi.ldap.connect.pool.initsize=20 -Dcom.sun.jndi.ldap.connect.pool.prefsize=20 -Dcom.sun.jndi.ldap.connect.pool.maxsize=100 -Dcom.sun.jndi.ldap.connect.pool.timeout=30000"


# Setup JBoss sepecific properties
#JAVA_OPTS="-Dprogram.name=${SERVER} $JAVA_OPTS ${LDAP}"
JAVA_OPTS="-Dprogram.name=${SERVER} -Dlog.dir=${LOGDIR} $JAVA_OPTS"

# Setup the java endorsed dirs
JBOSS_ENDORSED_DIRS="$JBOSS_HOME/lib/endorsed"

#ARGS="-Dhostname=$HOSTNAME -Ddata.dir=/data/${SERVER} -Dlog.dir=/log/${SERVER}"

# kill JBoss process
JBOSS_PID=$(cat $pid)
kill $JBOSS_PID 2>/dev/null
r=$?
if (( $r == 0 ))
then
        /bin/echo -n "shutdown running instance"
fi
while (( $r == 0 ))
do
        /bin/echo -n "."
        sleep 3
        kill $JBOSS_PID 2>/dev/null
        r=$?
done

echo "rotate server logs"
touch ${LOGFILE}1
/bin/mv -f  ${LOGFILE}1 ${LOGFILE}2
touch ${LOGFILE}0
/bin/mv -f  ${LOGFILE}0 ${LOGFILE}1
touch ${LOGFILE}
/bin/mv -f  ${LOGFILE} ${LOGFILE}0
touch ${LOGFILE}

/bin/echo

# Display our environment
echo "========================================================================="
echo ""
echo "  JBoss Bootstrap Environment"
echo ""
echo "  SERVER: $SERVER"
echo "  Host IP: $hostIp"
echo "  JVM Debug Port: $jvmDebugPort"
echo "  JMX Remote Port: $jmxRemotePort"
echo ""
echo "  JBOSS_HOME: $JBOSS_HOME"
echo ""
echo "  JAVA: $JAVA"
echo ""
echo "  LOGFILE: $LOGFILE"
echo ""
echo "  JAVA_OPTS: $JAVA_OPTS $LOG_OPTS"
echo ""
#echo "  ARGUMENTS: $ARGS"
#echo ""
echo "  CLASSPATH: $CLASSPATH"
echo ""
echo "========================================================================="
echo ""

# note that "-b0.0.0.0" binds to all ports.
# Execute the JVM in the background
"$JAVA" $JAVA_OPTS $LOG_OPTS\
    -Djava.endorsed.dirs="$JBOSS_ENDORSED_DIRS" \
    -classpath "$CLASSPATH" \
    ${ARGS} \
    org.jboss.Main "--host=${hostIp}" "--configuration=${SERVER}" > $LOGFILE 2>&1 &
    JBOSS_PID=$!

echo $JBOSS_PID > $pid
echo "You can follow along at home by running:"
echo "tail -f $LOGFILE"


example shutdown script stopJBoss.sh:

#!/bin/sh
### ====================================================================== ###
##                                                                          ##
##  JBoss Bootstrap Script                                                  ##
##                                                                          ##
### ====================================================================== ###

echo "Stopping JBoss"

PROGNAME=`basename $0`
whoiam=$(/usr/bin/id --user --name)
if [[ "$whoiam" != "jbadmin" ]]
then
        echo "$PROGNAME: This script must be run as jbadmin."
        exit -1
fi

export JAVA_HOME="/apps/java/default"
export JBOSS_HOME="/apps/jboss/default"
export PATH=${JAVA_HOME}/bin:${PATH}
JAVA=${JAVA_HOME}/bin/java

# make the JBOSS_HOME the real path not a slimebolic link.
WD=$(/bin/pwd)
cd ${JBOSS_HOME}
jb=$(/bin/pwd)
export JBOSS_HOME="$jb"
cd $WD

CLASSPATH="${JBOSS_HOME}/bin/shutdown.jar:${JBOSS_HOME}/client/jbossall-client.jar"
export CLASSPATH

# trick to find which server we are running
WD=$(/bin/pwd)

case "${0}" in
    /*)
        cmdDir=$(dirname "$0")
        ;;
    *)
        cmdDir=$(dirname "${WD}/$0")
        ;;
esac

cd  $cmdDir
wrkDir=$(/bin/pwd)
SERVER=$(echo $wrkDir| sed -e "s^${JBOSS_HOME}/server/^^" | sed -e "s^/.*^^")
SERVERDIR="${JBOSS_HOME}/server/${SERVER}"
pid=${SERVERDIR}/${SERVER}.pid
LOGFILE="${SERVERDIR}/log/${SERVER}.log"
APPDIR="/apps/$SERVER/server"

#
# Helper to complain.
#
warn() {
        echo "${PROGNAME}: $*"
}

#
# Helper to puke.
#
die() {
        warn $*
        exit 1
}

# The properties file for setting up hostIp, jvmDebugPort, and jmxRemotePort
setup="$APPDIR/${SERVER}/${SERVER}-server.properties"

if [[ -f ${setup} ]]
then
        echo "Reading server setup from ${setup}"
        hostIp=""
        source ${setup}

    if [[ ! $hostIp ]]
    then
        # The host IP must exist on the server properties file.
        die "Mandatory host IP is not found in $setup."
    fi
else
        die "Mandatory setup file (${setup}) does not exist.  Aborting."
fi

JBOSS_SERVER="${hostIp}:1099"
echo "Stopping server: $SERVER on $hostIp"
${JAVA} -classpath $CLASSPATH org.jboss.Shutdown --server=$JBOSS_SERVER $@


No comments: