当前位置: 首页>>技术教程>>正文


在Linux上,将JBoss 7作为服务启动

, , , ,

问题描述

以前的JBoss版本包含一个脚本(例如jboss_init_redhat.sh),可以将其复制到/etc/init.d以便将其添加为服务-因此它将在启动时启动。我似乎在JBoss 7中找不到任何类似的脚本。有人做过这样的事情吗?

附言我正在尝试在Ubuntu 10.04中实现这一目标

最佳办法

在花了几个小时的侦听之后,我最终创建了/etc/init.d/jboss,内容如下

#!/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

这是java.sh的内容:

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

jboss.sh

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

显然,您需要确保将JAVA_HOME和JBOSS_HOME设置为适合您的环境。

然后我运行了sudo update-rc.d jboss defaults,以便JBoss在系统启动时自动启动

我发现this article对创建上面的start-up脚本有帮助。同样,以上脚本适用于Ubuntu(在我的情况下为10.04版),因此在Fedora /RedHat或CentOS中使用它可能无法正常工作(注释中的设置与此不同)。

次佳办法

我还拍摄了Ubuntu 10.04 LTS的脚本。 JBoss版本是7.1.1。我想要一个能够真正测试JBoss成功启动并能够相对正常关闭JBoss的脚本。我的起点是JBoss 7.1.1下载的bin /init.d目录中包含的JBoss脚本。我使用了此页面上其他脚本以及其他Ubuntu初始化脚本中的一些内容进行了调整。请注意,Ubuntu使用破折号作为其默认的init-script解释器,而不是JBoss脚本显然期望的bash。具有中等重要性的是,日志记录发生在/var/log/jboss-as而不是${JBOSS_HOME}/standalone/log中。该日志还用于确定JBoss是否成功启动。

jboss-as-standalone.sh

#!/bin/sh
#
# JBoss standalone control script
#
# Provided in JBoss AS 7.1.1
# Modified for Ubuntu Server 10.04 by koma
#
# chkconfig: - 80 20
# description: JBoss AS Standalone
# processname: standalone
# pidfile: /var/run/jboss-as/jboss-as-standalone.pid
# config: /etc/default/jboss-as
#
### BEGIN INIT INFO
# Provides:          jboss-as
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start JBoss AS
# Description:       Start JBoss Application Server.
### END INIT INFO
#

# Inspired by tomcat6 init script, might be somewhat redundant
#PATH=???
NAME=jboss-as
DESC="JBoss AS"
DEFAULT=/etc/default/$NAME

# Source function library.
#. /etc/init.d/functions
# Ubuntu has it here (but probably different !)
. /lib/lsb/init-functions

# Load Java configuration.
# Ubuntu has it in /etc/default
[ -r /etc/default/java ] && . /etc/default/java
export JAVA_HOME

# Load JBoss AS init.d configuration.
if [ -z "$JBOSS_CONF" ]; then
# Ubuntu: seems more logical there
  JBOSS_CONF="/etc/default/jboss-as"
fi

[ -r "$JBOSS_CONF" ] && . "${JBOSS_CONF}"

# Set defaults.

if [ -z "$JBOSS_HOME" ]; then
  JBOSS_HOME="/var/local/jboss-as"
fi
export JBOSS_HOME

# might be unbeautiful
# this made chown fail because JBOSS_USER was empty
if [ -z "$JBOSS_USER" ]; then
  JBOSS_USER="jboss"
fi
export JBOSS_USER

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
# use JBOSS_LOG_DIR from jboss script instead
if [ -z "$JBOSS_LOG_DIR" ]; then
  JBOSS_LOG_DIR=/var/log/jboss-as
fi
export JBOSS_LOG_DIR

# We need this to be set to get a pidfile !
if [ -z "$LAUNCH_JBOSS_IN_BACKGROUND" ]; then
  LAUNCH_JBOSS_IN_BACKGROUND=true
fi
export LAUNCH_JBOSS_IN_BACKGROUND

if [ -z "$STARTUP_WAIT" ]; then
  STARTUP_WAIT=120
fi

if [ -z "$SHUTDOWN_WAIT" ]; then
  SHUTDOWN_WAIT=120
fi

if [ -z "$JBOSS_CONFIG" ]; then
  JBOSS_CONFIG=standalone.xml
fi

JBOSS_SCRIPT=$JBOSS_HOME/bin/standalone.sh

prog='jboss-as'


start() {
  log_daemon_msg "Starting $DESC"
  id $JBOSS_USER > /dev/null 2>&1
  if [ $? -ne 0 -o -z "$JBOSS_USER" ]; then
    log_failure_msg "User $JBOSS_USER does not exist..."
    log_end_msg 1
    exit 1
  fi
  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
      log_progress_msg "$prog is already running"
      log_end_msg 0
      exit 0
    else
      rm -f $JBOSS_PIDFILE
    fi
  fi
  mkdir -p $JBOSS_LOG_DIR
  # not sure: clear boot.log ... dunno if good, dunno if hardcoding boot.log good
  cat /dev/null > ${JBOSS_LOG_DIR}"/boot.log"
  # same as for boot.log, but we need to clear server.log to get proper launch detection (grepping later)
  cat /dev/null > ${JBOSS_LOG_DIR}"/server.log"
  chown -R ${JBOSS_USER}: $JBOSS_LOG_DIR

  mkdir -p $(dirname $JBOSS_PIDFILE)
  chown ${JBOSS_USER}: $(dirname $JBOSS_PIDFILE) || true

  if [ ! -z "$JBOSS_USER" ]; then
    start-stop-daemon --start -b -u "$JBOSS_USER" -c "$JBOSS_USER" -d "$JBOSS_HOME" -p "$JBOSS_PIDFILE" -x ${JBOSS_HOME}/"bin/standalone.sh" -- -Djboss.server.log.dir="$JBOSS_LOG_DIR"
  else
    log_failure_msg "Error: Environment variable JBOSS_USER not set or empty."
    log_end_msg 1
    exit 1
  fi

  count=0
  launched=false

  until [ $count -gt $STARTUP_WAIT ]
  do
    grep 'JBoss AS.*started in' ${JBOSS_LOG_DIR}"/server.log" > /dev/null 
    if [ $? -eq 0 ] ; then
      launched=true
      break
    fi
    sleep 1
    count=$((count+1));
  done

  if [ $launched=true ]; then
    if [ -f $JBOSS_PIDFILE ] && [ -s $JBOSS_PIDFILE ]; then
      log_progress_msg "Successfully started $DESC."
    else
      log_progress_msg "Successfully started $DESC, but problems with pidfile."
    fi
  else
    log_failure_msg "Launching $DESC failed."
    # If the pidfile exists, try to kill the process
    if [ -f $JBOSS_PIDFILE ] && [ -s $JBOSS_PIDFILE ]; then
      read kpid < $JBOSS_PIDFILE
      log_progress_msg "Pidfile detected. Please take care of process $kpid manually."
    fi
    log_end_msg 1
    exit 1
  fi

  # success
  log_end_msg 0
  return 0
}

stop() {
  log_daemon_msg "Stopping $DESC"
  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=$((count+1));
    done

    if [ $count -gt $kwait ]; then
      kill -9 $kpid
    fi
  fi
  rm -f $JBOSS_PIDFILE
  log_end_msg 0
  return 0
}

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
      log_success_msg "$prog is running (pid $ppid)"
      exit 0
    else
      log_success_msg "$prog dead but pid file exists"
      exit 1
    fi
  fi
  log_success_msg "$prog is not running"
  exit 3
}

reload() {
  log_begin_msg "Reloading $prog ..."
  start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/jboss-cli.sh -- --connect command=:reload
  log_end_msg $?
  exit $?
}

case "$1" in
  start)
      start
      ;;
  stop)
      stop
      ;;
  restart)
      $0 stop
      $0 start
      ;;
  status)
      status
      ;;
  reload)
      reload
      ;;
  *)
      ## If no parameters are given, print which are avaiable.
      echo "Usage: $0 {start|stop|status|restart|reload}"
      exit 1
      ;;
esac

以及随附的配置(将其放入/etc /default /jboss-as):

# JBoss AS configuration
JBOSS_HOME="/var/local/jboss-as"
JBOSS_USER=jboss

有效的方法(命令,从/etc/init.d/jboss-as链接脚本后使用sudo service jboss-as <command>):

  • 启动(阻塞,直到服务器成功启动)

  • 停止(也阻止)

  • 重新开始

  • 状态

未测试的内容:

  • 如果重新加载正常

  • 如果JBoss失败怎么办

  • 其他几种可能失败的情况

我还没有深入探讨几个主题,尤其是:

  • set + e /set -e的作用到底是什么,并且是必需的(在其他脚本中使用)

  • log _ * _ msg函数的语义是什么,应在哪里使用?

  • 如何正确执行日志记录工作(将位置传递给JBoss)

  • 如果jboss-cli.sh脚本可以并且应该用于更多事情(例如启动/运行检测)

  • 如果确实有必要一直将standalone.sh作为进程运行

  • 如果返回值合适

请注意,我绝对不是shell脚本专家-我相当无知。因此,如果您在代码中发现任何愚蠢的想法,或者对改进有任何想法,我将很高兴听到它们。

第三种办法

最近,我为JBoss AS 7编写了安装程序,该安装程序从RedHat的服务器下载tar.gz文件,将其解压缩,添加jboss-as作为服务并进行一些非常基本的配置。有了它,我可以在几秒钟内准备使用JBoss AS 7。

安装脚本:

#!/bin/bash
#title           :jboss-install.sh
#description     :The script to install JBoss AS 7.x
#author          :Dmitriy Sukharev
#date            :20130106
#usage           :/bin/bash jboss-install.sh

JBOSS_AS_FILENAME=jboss-as-7.1.1.Final
JBOSS_AS_ARCHIVE_NAME=$JBOSS_AS_FILENAME.tar.gz 
JBOSS_AS_DOWNLOAD_ADDRESS=http://download.jboss.org/jbossas/7.1/$JBOSS_AS_FILENAME/$JBOSS_AS_ARCHIVE_NAME

INSTALL_DIR=/opt
JBOSS_AS_FULL_DIR=$INSTALL_DIR/$JBOSS_AS_FILENAME
JBOSS_AS_DIR=$INSTALL_DIR/jboss-as

JBOSS_AS_USER="jboss-as"
JBOSS_AS_SERVICE="jboss-as"

JBOSS_AS_STARTUP_TIMEOUT=240

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "Cleaning up..."
rm "$JBOSS_AS_ARCHIVE_NAME"
rm "$JBOSS_AS_DIR"
rm -r "$JBOSS_AS_FULL_DIR"
rm -r "/var/run/$JBOSS_AS_SERVICE/"
rm /etc/init.d/$JBOSS_AS_SERVICE

echo "Installation..."
wget $JBOSS_AS_DOWNLOAD_ADDRESS
mkdir $JBOSS_AS_FULL_DIR
tar -xzf $JBOSS_AS_ARCHIVE_NAME -C $INSTALL_DIR
ln -s $JBOSS_AS_FULL_DIR/ $JBOSS_AS_DIR
useradd -s /sbin/nologin $JBOSS_AS_USER
chown -R $JBOSS_AS_USER:$JBOSS_AS_USER $JBOSS_AS_DIR
chown -R $JBOSS_AS_USER:$JBOSS_AS_USER $JBOSS_AS_DIR/
rm $JBOSS_AS_ARCHIVE_NAME

echo "Registrating JBoss as service..."
sed -e 's,${JBOSS_AS_USER},'$JBOSS_AS_USER',g; s,${JBOSS_AS_FILENAME},'$JBOSS_AS_FILENAME',g; s,${JBOSS_AS_SERVICE},'$JBOSS_AS_SERVICE',g; s,${JBOSS_AS_DIR},'$JBOSS_AS_DIR',g' $SCRIPT_DIR/jboss-as.template > /etc/init.d/$JBOSS_AS_SERVICE
chmod 755 /etc/init.d/$JBOSS_AS_SERVICE

echo "Configurating..."
sed -i -e 's,<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000"/>,<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000" deployment-timeout="'$JBOSS_AS_STARTUP_TIMEOUT'"/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<virtual-server name="default-host" enable-welcome-root="true">,<virtual-server name="default-host" enable-welcome-root="false">,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<inet-address value="${jboss.bind.address:127.0.0.1}"/>,<any-address/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<socket-binding name="ajp" port="8009"/>,<socket-binding name="ajp" port="28009"/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<socket-binding name="http" port="8080"/>,<socket-binding name="http" port="28080"/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<socket-binding name="https" port="8443"/>,<socket-binding name="https" port="28443"/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml
sed -i -e 's,<socket-binding name="osgi-http" interface="management" port="8090"/>,<socket-binding name="osgi-http" interface="management" port="28090"/>,g' $JBOSS_AS_DIR/standalone/configuration/standalone.xml

echo "Done."

初始化脚本:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          ${JBOSS_AS_SERVICE}
# 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_FILENAME}
### END INIT INFO

JBOSS_USER=${JBOSS_AS_USER}
JBOSS_DIR=${JBOSS_AS_DIR}

case "$1" in
start)
echo "Starting ${JBOSS_AS_FILENAME}..."
start-stop-daemon --start --background --chuid $JBOSS_USER --exec $JBOSS_DIR/bin/standalone.sh
exit $?
;;
stop)
echo "Stopping ${JBOSS_AS_FILENAME}..."

start-stop-daemon --start --quiet --background --chuid $JBOSS_USER --exec $JBOSS_DIR/bin/jboss-cli.sh -- --connect command=:shutdown
exit $?
;;
log)
echo "Showing server.log..."
tail -500f $JBOSS_DIR/standalone/log/server.log
;;
*)
echo "Usage: /etc/init.d/jboss-as {start|stop}"
exit 1
;;
esac
exit 0

我在blog post中描述了脚本步骤。它还具有用于将此脚本文件下载为存档的链接。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/9810.html,未经允许,请勿转载。