Start JBoss 7 as a service on Linux

后端 未结 12 1986
闹比i
闹比i 2020-12-12 11:54

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 star

相关标签:
12条回答
  • 2020-12-12 12:25

    Just been fighting through this tonight, and thought I'd share my findings. In the end i followed the install instructions here: http://ptoconnor.wordpress.com/2012/11/19/jboss-as-7-1-1-on-an-ubuntu-12-04-aws-instance-running-oracle-java-7/ with some alterations;

    I installed to /usr/share not /opt

    To get jboss to run as a service, i created a symbolic link to the redhat/centos friendly /usr/share/jboss-as-7.1.1.Final/bin/init.d/jboss-as-standalone.sh which was provided with the 7.1.1 final release

    sudo ln -s /usr/share/jboss-as-7.1.1.Final/bin/init.d/jboss-as-standalone.sh /etc/init.d/jboss
    

    then a few changes to make it ubuntu friendly

    #!/bin/sh
    #
    # JBoss standalone control script
    #
    # chkconfig: - 80 20
    # description: JBoss AS Standalone
    # processname: standalone
    # pidfile: /var/run/jboss-as/jboss-as-standalone.pid
    # config: /etc/jboss-as/jboss-as.conf
    
    # Source function library.
    . /lib/lsb/init-functions
    
    # Load Java configuration.
    [ -r /etc/java/java.conf ] && . /etc/java/java.conf
    export JAVA_HOME
    
    # Load JBoss AS init.d configuration.
    if [ -z "$JBOSS_CONF" ]; then
      JBOSS_CONF="/etc/jboss-as/jboss-as.conf"
    fi
    
    [ -r "$JBOSS_CONF" ] && . "${JBOSS_CONF}"
    
    # Set defaults.
    
    if [ -z "$JBOSS_HOME" ]; then
      JBOSS_HOME=/usr/share/jboss-as
    fi
    export JBOSS_HOME
    
    if [ -z "$JBOSS_PIDFILE" ]; then
      JBOSS_PIDFILE=/var/run/jboss-as/jboss-as-standalone.pid
    fi
    export JBOSS_PIDFILE
    
    if [ -z "$JBOSS_CONSOLE_LOG" ]; then
      JBOSS_CONSOLE_LOG=/var/log/jboss-as/console.log
    fi
    
    if [ -z "$STARTUP_WAIT" ]; then
      STARTUP_WAIT=30
    fi
    
    if [ -z "$SHUTDOWN_WAIT" ]; then
      SHUTDOWN_WAIT=30
    fi
    
    if [ -z "$JBOSS_CONFIG" ]; then
      JBOSS_CONFIG=standalone.xml
    fi
    
    JBOSS_SCRIPT=$JBOSS_HOME/bin/standalone.sh
    
    prog='jboss-as'
    
    CMD_PREFIX=''
    
    JBOSS_USER=jboss
    
    if [ ! -z "$JBOSS_USER" ]; then
      if [ -x /lib/lsb/init-functions ]; then
        CMD_PREFIX="start-stop-daemon -user $JBOSS_USER"
      else
        CMD_PREFIX="su - $JBOSS_USER -c"
      fi
    fi
    
    start() {
      echo -n "Starting $prog: "
      if [ -f $JBOSS_PIDFILE ]; then
        read ppid < $JBOSS_PIDFILE;
        if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
          echo -n "$prog is already running"
          failure
          echo
          return 1 
        else
          rm -f $JBOSS_PIDFILE
        fi
      fi
      mkdir -p $(dirname $JBOSS_CONSOLE_LOG)
      cat /dev/null > $JBOSS_CONSOLE_LOG
    
      mkdir -p $(dirname $JBOSS_PIDFILE)
      chown $JBOSS_USER $(dirname $JBOSS_PIDFILE) || true
      #$CMD_PREFIX JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT 2>&1 > $JBOSS_CONSOLE_LOG &
      #$CMD_PREFIX JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT &
    
      if [ ! -z "$JBOSS_USER" ]; then
        if [ -x /lib/lsb/init-functions ]; then
          start-stop-daemon -user $JBOSS_USER LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG 2>&1 > $JBOSS_CONSOLE_LOG &
        else
          su - $JBOSS_USER -c "LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG" 2>&1 > $JBOSS_CONSOLE_LOG &
        fi
      fi
    
      count=0
      launched=false
    
      until [ $count -gt $STARTUP_WAIT ]
      do
        grep 'JBoss AS.*started in' $JBOSS_CONSOLE_LOG > /dev/null 
        if [ $? -eq 0 ] ; then
          launched=true
          break
        fi 
        sleep 1;
        count=`expr $count + 1`
      done
    
      printf success
      echo
      return 0
    }
    
    stop() {
      echo -n "Stopping $prog: "
      count=0;
    
      if [ -f $JBOSS_PIDFILE ]; then
        read kpid < $JBOSS_PIDFILE;
        kwait=$SHUTDOWN_WAIT
    
        # Try issuing SIGTERM
    
        kill -15 $kpid
        until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
        do
          sleep 1;
          count=`expr $count + 1`
        done
    
        if [ $count -gt $kwait ]; then
          kill -9 $kpid
        fi
      fi
      rm -f $JBOSS_PIDFILE
      printf success
      echo
    }
    
    status() {
      if [ -f $JBOSS_PIDFILE ]; then
        read ppid < $JBOSS_PIDFILE
        if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
          echo "$prog is running (pid $ppid)"
          return 0
        else
          echo "$prog dead but pid file exists"
          return 1
        fi
      fi
      echo "$prog is not running"
      return 3
    }
    
    case "$1" in
      start)
          start
          ;;
      stop)
          stop
          ;;
      restart)
          $0 stop
          $0 start
          ;;
      status)
          status
          ;;
      *)
          ## If no parameters are given, print which are avaiable.
          echo "Usage: $0 {start|stop|status|restart|reload}"
          exit 1
          ;;
    esac
    

    now it's just a case of installing the jboss service using the above script.

    sudo update-rc.d jboss defaults
    

    I know there are lots of variations now, but hopefully this will help the next person searching. All i wanted was a JBPM host ...

    0 讨论(0)
  • 2020-12-12 12:27

    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).

    0 讨论(0)
  • 2020-12-12 12:27

    Here's mine for gentoo. Not perfect yet but pretty clean and working well enough for me. First one small change to the jboss install:

    ~ # JBOSS_HOME=/opt/jboss   # or whatever you have it as
    ~ # echo "LAUNCH_JBOSS_IN_BACKGROUND=true"  >> "${JBOSS_HOME}"/bin/standalone.conf
    

    .

    ~ # cat /etc/conf.d/jboss
    JBOSS_HOME=/opt/jboss
    JBOSS_USER=jboss
    JBOSS_PIDFILE=/var/run/jboss/jboss.pid
    JBOSS_EXECUTABLE="${JBOSS_HOME}"/bin/standalone.sh
    JBOSS_STDOUT_LOG=/var/log/jboss/stdout.log
    JBOSS_STDERR_LOG=/var/log/jboss/stderr.log
    JBOSS_SHUTDOWN_WAIT_SECONDS=60
    

    .

    ~ # cat /etc/init.d/jboss
    #!/sbin/runscript
    
    depend()  {
            need net
    }
    
    start() {
            ebegin "Starting JBoss"
            start-stop-daemon -S -b -m -p "${JBOSS_PIDFILE}" -u "${JBOSS_USER}" -x "${JBOSS_EXECUTABLE}" -1 "${JBOSS_STDOUT_LOG}" -2 "${JBOSS_STDERR_LOG}"
            eend $?
    } 
    
    stop() {
            ebegin "Stopping JBoss"
            start-stop-daemon -K -p "${JBOSS_PIDFILE}" -u "${JBOSS_USER}" -R ${JBOSS_SHUTDOWN_WAIT_SECONDS}
            eend $?
    }
    

    I can't get startup to say [ OK ] as soon as the deployments all finish. I've tried a few things but no luck yet - it either waits forever or currently just says [ OK ] as soon as the shell script is forked. Stopping is better, as long as you set the delay long enough. Log rotation would be pretty easy to add

    0 讨论(0)
  • 2020-12-12 12:29

    The answer marked as correct here did not work for me. On restart, you get a security error related to the usage of sudo, stating, "sorry, you must have a tty to run sudo." Further research revealed that disabling the sudo tty restriction could cause plain text exposure of passwords, so that's no good.

    Here's what I ended up with and it works fine for me:

    #!/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"
            su --session-command "${JBOSS_HOME}/bin/standalone.sh >& /dev/null &" jboss
        ;;
        stop)
            echo "Stopping JBoss AS 7.0.0"
            su --session-command "${JBOSS_HOME}/bin/jboss-admin.sh --connect command=:shutdown" jboss
        ;;
        *)
            echo "Usage: /etc/init.d/jboss {start|stop}"
            exit 1
        ;;
    esac
    
    exit 0
    
    0 讨论(0)
  • 2020-12-12 12:30

    Watch under bin directory you have init.d/jboss-as-standalone.sh (jboss-as-7.1.0.CR1b)

    0 讨论(0)
  • 2020-12-12 12:30

    To print PID of command executed use shell variable $!. This variable will print PID of process executed.

    case "$1" in
        start)
            echo "Starting JBoss AS 7.0.0"
            su --session-command "${JBOSS_HOME}/bin/standalone.sh >& /dev/null &" jboss
            echo $! > /tmp/jboss.pid
        ;;
    
    0 讨论(0)
提交回复
热议问题