Version 30

    How do I Start JBoss AS 7 / JBoss EAP 6 on startup (as a service) with Linux?

    See How to install JBoss AS 7 as a Linux service.

    How do I Start JBoss AS 4, 5, 6 on startup (as a service) with Linux?

     

    Overview

     

    Linux uses System V init scripts.  Although there can be some differences between distributions, generally it looks like this:

     

    • /etc/rc.d/init.d/ - contains the start and stop scripts (other distributions: /etc/init.d/)

    • /etc/rc.(x)/ - contains links to the start and stop scripts prefixed with S or K (start or kill respectively)

     

    There are various run levels for various stages of system use.

     

    1. rc1.d - Single User Mode

    2. rc2.d - Single User Mode with Networking

    3. rc3.d - Multi-User Mode - boot up in text mode

    4. rc4.d - Undefined

    5. rc5.d - Multi-User Mode - boot up in X Windows

    6. rc6.d - Shutdown

     

    Most Linux systems used as Application Servers boot into run level 3 (if not, your system admin needs to be questioned on why your server needs to boot into X-Windows and needlessly waste system resources).

     

    Your task is to:

     

    • create a user for JBoss (recommended) so that JBoss can be restricted to accessing only the files and system resources that it has permission to access via the "jboss" user.

    • create a script called /etc/rc.d/init.d/jboss

    • create a link called /etc/rc3.d/S84jboss

      • optionally /etc/rc5.d/S84jboss and /etc/rc4.d/S84jboss

    • create a link called /etc/rc6.d/K15jboss

      • create the K15 link in /etc/rc1.d, /etc/rc2.d, /etc/rc0.d

    -


    Create a user called "jboss" (recommended)

     

    As root type "adduser --system jboss" and enter a password in case you later want to login as user jboss; Normally you will treat jboss like the users apache, www, postgres,... and disable direct login. Su to jboss and install the server in $JBOSS_HOME. Make sure the $JBOSS_HOME directory can be read by "jboss" and that the $JBOSS_HOME/server/default/work directory can be read and written. In case some permissions are messed up check the RecommendedUNIXFilesystemPermissionsForJBossApplicationServer.

     

    Note that SELinux environment uses "runuser" instead of "su" (see below). "runuser" requires a

    valid shell like "bin/sh" for your JBOSS user.

     

    -


    Create the script in /etc/rc.d/init.d

     

    As root (su - root) type vi /etc/rc.d/init.d/jboss and paste the below:

     

    #! /bin/sh
    
    
    start(){
            echo "Starting jboss.."
    
            # If using an SELinux system such as RHEL 4, use the command below
            # instead of the "su":
            # eval "runuser - jboss -c '/opt/jboss/current/bin/run.sh > /dev/null 2> /dev/null &'
            # if the 'su -l ...' command fails (the -l flag is not recognized by my su cmd) try:
            #   sudo -u jboss /opt/jboss/bin/run.sh > /dev/null 2> /dev/null &
            su -l jboss -c '/opt/jboss/current/bin/run.sh > /dev/null 2> /dev/null &'
    }
    
    stop(){
            echo "Stopping jboss.."
    
            # If using an SELinux system such as RHEL 4, use the command below
            # instead of the "su":
            # eval "runuser - jboss -c '/opt/jboss/current/bin/shutdown.sh -S &'
            # if the 'su -l ...' command fails try:
            #   sudo -u jboss /opt/jboss/bin/shutdown.sh -S &
            su -l jboss -c '/opt/jboss/current/bin/shutdown.sh -S &'
    }
    
    restart(){
            stop
    # give stuff some time to stop before we restart
            sleep 60
    # protect against any services that can't stop before we restart (warning this kills all Java instances running as 'jboss' user)
            su -l jboss -c 'killall java'
    # if the 'su -l ...' command fails try:
            #   sudo -u jboss killall java
            start
    }
    
    
    
    
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      restart)
            restart
            ;;
      *)
            echo "Usage: jboss {start|stop|restart}"
            exit 1
    esac
    
    exit 0
    

     

    Also as root type "chmod 755 /etc/rc.d/init.d/jboss" in order to make the script executable.  You should be able to test it by typing "service jboss stop", "service jboss start" and so forth.  Note that the script hard codes $JBOSS_HOME so make sure that it reflects your JBOSS_HOME.  Because the console output is also in the server.log we're just sending it to /dev/null.

     

    The restart target in this script does not perform the same actions as a stop followed by a start; it contains a hard-coded 60-second sleep before killing Java the hard way.  This has several negative effects, including:

    • making a restart take at least 60 seconds (incurring at least 60 seconds service downtime)

    • potentially aborting a JBoss instance that would have shut down of its own accord in due course (the corollary of this is that the stop target is not guaranteed to stop the running JBoss instance)

     

     

    Using the included init Scripts (JBoss 4.0.1 and higher)

    Alternatively, JBoss 4.0.1 (and higher) comes with prebaked init scripts in the bin directory, jboss_init_redhat.sh and jboss_init_suse.sh.  You can copy one of these scripts to /etc/rc.d/init.d/jboss, then make the links below, or create a symbolic link from /etc/rc.d/init.d/jboss to one of them.  These scripts don't pipe logging to /dev/null, but to a real file, so you'll have to add one additional step:

     

    mkdir $JBOSS_HOME/log
    chown jboss $JBOSS_HOME/log
    

     

    You will also need to modify the start and stop commands to use "runuser" instead of "su" if using a SELinux distribution such as RHEL 4.

     

    example modifications:

    JBOSSUS=${JBOSSUS:-"jboss"}                        # put this before JBOSS_CONSOLE def                                
    SUBIT="runuser - $JBOSSUS -c "                     # replace su by runuser due to SELinux
    JBOSS_CONSOLE="$JBOSS_HOME/log/jboss.log"          # instead of nothing 
    chown $JBOSSUS $JBOSS_CONSOLE                      # make log writable by JBOSS user               
    JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c default"} # change "all" for "default"  
    

     

    -


    create links

     

    The links will be used to identify at which run levels JBoss should be started and stopped.  In general this is probably what you want (do as root):

     

    ln -s /etc/rc.d/init.d/jboss /etc/rc3.d/S84jboss
    ln -s /etc/rc.d/init.d/jboss /etc/rc5.d/S84jboss 
    ln -s /etc/rc.d/init.d/jboss /etc/rc4.d/S84jboss
    
    ln -s /etc/rc.d/init.d/jboss /etc/rc6.d/K15jboss 
    ln -s /etc/rc.d/init.d/jboss /etc/rc0.d/K15jboss
    ln -s /etc/rc.d/init.d/jboss /etc/rc1.d/K15jboss
    ln -s /etc/rc.d/init.d/jboss /etc/rc2.d/K15jboss
    

     

    Linux will execute the equivilent of "service jboss start" for the "S" links and "service jboss stop" for the K links.

     

    RedHat has a chkconfig command to manage these links, which may or may not work (it uses comments in the top of the script to determine which run-levels it should be started/stopped in).  For Debian or Ubuntu, use update-rc.d(8).  For SuSe, use the insserv command to properly register the new jboss script--skip the symlinking you see above, this will be ignored and will not run JBoss at boot time).

     

    -


    Using Tanuki Software's Java Service Wrapper

     

    Have a look at http://wrapper.tanukisoftware.org/ - the Java Service Wrapper is a very powerful service wrapper for any Java application and works fine with JBoss.

     

    You can also have a look at the Windows Java Service page, which has more instructions and configuration options for the Java Service Wrapper. 

     

    A basic wrapper.conf would look like this:

     

    wrapper.java.command=/usr/java/bin/java
    wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
    
    wrapper.java.classpath.1=../lib/wrapper.jar
    wrapper.java.classpath.2=run.jar
    wrapper.java.classpath.3=/usr/java/lib/tools.jar
    
    wrapper.java.library.path.1=../lib
    
    wrapper.java.additional.1=-server
    wrapper.java.additional.2=-Dprogram.name=run.sh
    
    wrapper.java.initmemory=256
    wrapper.java.maxmemory=256
    
    wrapper.app.parameter.1=org.jboss.Main
    wrapper.app.parameter.2=-c
    wrapper.app.parameter.3=default
    
    wrapper.console.format=PM
    wrapper.console.loglevel=INFO
    
    wrapper.logfile=wrapper.log
    wrapper.logfile.format=LPTM
    wrapper.logfile.loglevel=INFO
    wrapper.logfile.maxsize=1m
    wrapper.logfile.maxfiles=1
    
    wrapper.syslog.loglevel=NONE