JBoss automatic startup in Ubuntu 9.10

I come from Windows background, where I start and stop my development app server instance conveniently from Eclipse and view the server and app log directly as the standard console output.

JBoss has been running on my linux server for about 4 months now, but I never found time to make it start automatically at system startup. I had to start it manually with run.sh all the time. Now when I’m transferring it to other hardware and system and I’m configuring everything all over again, I decided finally to settle this matter once and for all. I’m runnig JBoss AS 5.0.1 GA. So this is the story. It uses my real directories, because I don’t want to spoil the narration by expressions like %JBOSS_HOME%.

My JBoss resides in /data/server/jboss and the configuration I’m running is default” (of course I edited it for my purpose, and mostly it is based on “web” configuration).

So the main startup script is /data/server/jboss/bin/run.sh

I edited /data/server/jboss/bin/run.conf to my liking (set the JAVA, JAVA_HOME vars, set 512MB memory via JAVA_OPTS and added pluggable-instrumentor.jar for JBoss AOP)

Then I took /data/server/jboss/bin/jboss_init_redhat.sh and copied it to /etc/init.d/jboss. The command that adds the symlinks to the /etc/rc?.d directories looks like this:

sudo update-rc.d -f jboss start 84 2 3 4 5 . stop 15 0 1 6 .

The numbers 84 for start(S) links and 15 for stop(K) links are taken from a JBoss community howto page.

But the update-rc.d script didn’t like it, because it missed some “LSB Information“. So I added this and my final script looked like this:

#!/bin/sh
#
# Modified JBoss Control Script

### BEGIN INIT INFO
# Provides:          jboss
# Required-Start:    $local_fs $syslog $network $named
# Required-Stop:     $local_fs $syslog $network $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start JBoss
# Description:       Starts JBoss Server
### END INIT INFO

#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/data/server/jboss"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"misoli"}

#make sure java is in your path
JAVAPTH=${JAVAPTH:-"/usr/lib/jvm/java-6-openjdk"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"default"}

JBOSS_HOST="linux"
JBOSS_JNP_PORT="1099"

#if JBOSS_HOST specified, use -b to bind jboss services to that address
JBOSS_BIND_ADDR=${JBOSS_HOST:+"-b $JBOSS_HOST"}

#define the classpath for the shutdown class
JBOSSCP=${JBOSSCP:-"$JBOSS_HOME/bin/shutdown.jar:$JBOSS_HOME/client/jnet.jar"}

#define the script to use to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF $JBOSS_BIND_ADDR"}

if [ "$JBOSS_USER" = "RUNASIS" ]; then
 SUBIT=""
else
 SUBIT="su - $JBOSS_USER -c "
fi

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then
 # ensure the file exists
 touch $JBOSS_CONSOLE
 if [ ! -z "$SUBIT" ]; then
 chown $JBOSS_USER $JBOSS_CONSOLE
 fi
fi

if [ -n "$JBOSS_CONSOLE" -a ! -f "$JBOSS_CONSOLE" ]; then
 echo "WARNING: location for saving console log invalid: $JBOSS_CONSOLE"
 echo "WARNING: ignoring it and using /dev/null"
 JBOSS_CONSOLE="/dev/null"
fi

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/dev/null"}

JBOSS_CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"
JBOSS_CMD_STOP=${JBOSS_CMD_STOP:-"java -classpath $JBOSSCP org.jboss.Shutdown --shutdown -s $JBOSS_HOST:$JBOSS_JNP_PORT"}

if [ -z "`echo $PATH | grep $JAVAPTH`" ]; then
 export PATH=$PATH:$JAVAPTH
fi

if [ ! -d "$JBOSS_HOME" ]; then
 echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
 exit 1
fi

echo JBOSS_CMD_START = $JBOSS_CMD_START

case "$1" in
start)
 cd $JBOSS_HOME/bin
 if [ -z "$SUBIT" ]; then
 eval $JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &
 else
 $SUBIT "$JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &"
 fi
 ;;
stop)
 if [ -z "$SUBIT" ]; then
 $JBOSS_CMD_STOP
 else
 $SUBIT "$JBOSS_CMD_STOP"
 fi
 ;;
restart)
 $0 stop
 $0 start
 ;;
*)
 echo "usage: $0 (start|stop|restart|help)"
esac

Please note that jboss should be run under it’s own user e.g. jboss. For convenience reasons I’m using my regular username, which is a little less safe.

Main gotchas that I’ve encountered

1. The main JBoss community site that explains this process said, that on most linux systems JBoss should run on runlevel 3, but that’s not the case with Ubuntu, where the normal (all features on) runlevel is 2.

2. For shutdown to work properly I had to add the command line option -s server:port to the shutdown part of the script, and turn on the JNP service. For me that meant setting the Port attribute for mbean “jboss:service=Naming” to the value 1099 (from original value -1, which is set in configuration “web” on which I based my “default”) in the file /data/server/jboss/server/default/conf/jboss-service.xml

Of course, for the oblivious, /etc/init.d/jboss has to be executable (chmod +x).

I also made a handy little shortcut for log viewing command: tail -20 /data/server/jboss/server/default/log/server.log (which is also conveniently viewable by Ubuntu util System > Administration > Log File Viewer as I discovered)