Thursday, June 07, 2012

Ubuntu Startup Script for Jboss as Non-root User

So ever since I started using Ubuntu I have been trying to figure out startup scripts. I think I finally cracked the code. Below is a startup script (saved in /etc/init.d/jboss ) that I use to start/stop a jboss instance on my ubuntu vm. To actually install I had to run this:

sudo update-rc.d jboss defaults

Notice that my jboss does NOT run as root and its just installed in my users home directory. I'm not a fan of running services as root. Here is the startup script:

#!/bin/sh

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

#
# this is a basic start stop script that can be run as
# $0 start or $0 stop or $0 restart
#

JBOSS_HOME=/home/mdeanda/install/jboss
JBOSS_SCRIPT=./bin/standalone.sh
USER=mdeanda

set -e

. /lib/lsb/init-functions


start()
{
 cd ${JBOSS_HOME}
 rm -f /var/log/jboss.out
 touch /var/log/jboss.out
 chown ${USER} /var/log/jboss.out
 su ${USER} -c ${JBOSS_SCRIPT} > /var/log/jboss.out &
}

stop()
{
 #ps ax | grep java | grep jboss | cut -d' ' -f2 | xargs kill
 su ${USER} -c "${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown"
}

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

1 comment:

TODD TRIMMER said...

Killing the Java process that runs JBoss AS isn't the cleanest way of doing things. You might have servlets, listeners, etc., that perform actions when applications are about to shutdown. Granted, sometimes the official way is non-responsive, in which case you do have to resort to kill.