Configuring JBoss 7 to run as *nix service

Usually you run jboss using $JBOSS_HOME/bin/standalone.sh, but if you want to run JBoss in background, or when you installing it to remote server you can run it as daemon(service).

$JBOSS_HOME is folder where your jboss is unpacked. JBoss 7 comes with prepared daemon script jboss-as-standalone.sh located in $JBOSS_HOME/bin/init.d. Here is how it look like by default. Two important things here is:

  1. set JAVA_HOME variable. This java will be used by JBoss.
  2. set JBOSS_CONF variable. This variable should point to a file where other variables will be set. This is optional variable, and you can set all variables in jboss-as-standalone.sh, but it would be easier to read and configure if you use JBOSS_CONF. Add this variables after 9-th line, where first comment block ended:

    JAVA_HOME=/usr/share/jvm/jdk1.8.0
    JBOSS_CONF=/home/c0rp/servers/jboss/bin/init.d/jboss-as.conf
    

Sample of jboss-as.conf is also located in $JBOSS_HOME/bin/init.d. This is sample jboss-as.conf.

# General configuration for the init.d scripts,
# not necessarily for JBoss AS itself.

# The username who should own the process.
#
# JBOSS_USER=jboss-as

# The amount of time to wait for startup
#
# STARTUP_WAIT=30

# The amount of time to wait for shutdown
#
# SHUTDOWN_WAIT=30

# Location to keep the console log
#
# JBOSS_CONSOLE_LOG=/var/log/jboss-as/console.log

Important variables that you should set in jboss-as.conf:

  1. JBOSS_HOME=/home/c0rp/servers/jboss
  2. JBOSS_PIDFILE="$JBOSS_HOME"/PID_FILE
  3. JBOSS_CONSOLE_LOG="$JBOSS_HOME"/standalone/log/console.log
  4. JBOSS_CONFIG="$JBOSS_HOME"/standalone/configuration/standalone.xml
  5. JBOSS_USER=jboss_user

Now copy $JBOSS_HOME/bin/init.d/jboss-as-standalone.sh to /etc/init.d/jboss, make it executable and add using chkconfig:

cp $JBOSS_HOME/bin/init.d/jboss-as-standalone.sh /etc/init.d/jboss
chmod +x /etc/init.d/jboss
chkconfig --add jboss

Now you should be able to run:

service jboss start
service jboss stop