Start JBoss 7 as a service on Linux

Andrey picture Andrey · Jul 30, 2011 · Viewed 72.1k times · Source

Previous versions of JBoss included a scripts (like jboss_init_redhat.sh) that could be copied to /etc/init.d in order to add it as a service - so it would start on boot up. I can't seem to find any similar scripts in JBoss 7. Has anyone already done something like this?

P.S. I'm trying to achieve this in Ubuntu 10.04

Answer

Andrey picture Andrey · Jul 30, 2011

After spending a couple of hours of snooping around I ended up creating /etc/init.d/jboss with the following contents

#!/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: Start/Stop JBoss AS v7.0.0
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
[ -r /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
[ -r /etc/profile.d/jboss.sh ] && . /etc/profile.d/jboss.sh

case "$1" in
    start)
        echo "Starting JBoss AS 7.0.0"
        #original:
        #sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh

        #updated:
        start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
    ;;
    stop)
        echo "Stopping JBoss AS 7.0.0"
        #original:
        #sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh --connect command=:shutdown

        #updated:
        start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/jboss-admin.sh -- --connect command=:shutdown
    ;;
    *)
        echo "Usage: /etc/init.d/jboss {start|stop}"
        exit 1
    ;;
esac

exit 0

Here's the content of java.sh:

export JAVA_HOME=/usr/lib/jvm/java_current
export PATH=$JAVA_HOME/bin:$PATH

And jboss.sh:

export JBOSS_HOME=/opt/jboss/as/jboss_current
export PATH=$JBOSS_HOME/bin:$PATH

Obviously, you need to make sure, you set JAVA_HOME and JBOSS_HOME appropriate to your environment.

then I ran sudo update-rc.d jboss defaults so that JBoss automatically starts on system boot

I found this article to be helpful in creating the start-up script above. Again, the script above is for Ubuntu (version 10.04 in my case), so using it in Fedora/RedHat or CentOS will probably not work (the setup done in the comments is different for those).