在Linux上启动JBoss 7作为一个服务

时间:2023-01-19 10:59:56

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?

JBoss以前的版本包括一个脚本(比如jboss_init_redhat.sh),可以复制到/etc/ initsh。为了将它添加为服务——所以它将在启动时启动。我似乎在JBoss 7中找不到任何类似的脚本。有人做过这样的事吗?

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

附注:我试图在Ubuntu 10.04中实现这一点

12 个解决方案

#1


58  

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

在四处窥探了几个小时之后,我最终创建了/etc/ initt.com。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

Here's the content of java.sh:

以下是java.sh的内容:

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

And jboss.sh:

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

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

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

然后运行sudo update-rc。d jboss默认设置,以便jboss自动启动系统启动。

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

我发现这篇文章对创建上面的启动脚本很有帮助。同样,上面的脚本是针对Ubuntu的(在我的例子中是10.04版),所以在Fedora/RedHat或CentOS中使用它可能不会成功(在注释中所做的设置是不同的)。

#2


12  

I also took a shot at a script for Ubuntu 10.04 LTS. JBoss version is 7.1.1. I wanted a script that really tests for successful JBoss startup and that is able to shut down JBoss relatively gracefully. My starting point was the JBoss script included in the bin/init.d directory in the JBoss 7.1.1 download. I adapted this with some bits from other scripts on this page as well as other Ubuntu init scripts. Note that Ubuntu uses dash as its default init-script interpreter, not bash as apparently expected by the JBoss script. Of medium importance is that the logging takes place in /var/log/jboss-as instead of ${JBOSS_HOME}/standalone/log. The log is also used to determine if JBoss started up successfully.

我也尝试了一下Ubuntu 10.04 LTS的脚本。安装7.1.1 JBoss版本。我想要一个真正测试成功的JBoss启动的脚本,它能够相对优雅地关闭JBoss。我的出发点是bin/init中包含的JBoss脚本。在JBoss 7.1.1下载目录。我在这个页面上使用了其他脚本的一些片段,以及其他Ubuntu初始化脚本。注意,Ubuntu使用了dash作为默认的脚本解释器,而不是JBoss脚本所期待的bash。其中重要的一点是,日志记录发生在/var/log/jboss—而不是${JBOSS_HOME}/独立/日志。日志还用于确定JBoss是否成功启动。

jboss-as-standalone.sh

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

And the accompanying configuration (put it in /etc/default/jboss-as ):

以及相应的配置(将其放入/etc/default/jboss-as中):

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



What works (commands, use sudo service jboss-as <command> after linking the script from /etc/init.d/jboss-as):

什么是有效的(命令,使用sudo服务jbo -as ,链接来自/etc/init. dc /jboss-as的脚本):

  • Startup (blocking until the server started successfully)
  • 启动(阻塞,直到服务器成功启动)
  • Stopping (also blocking)
  • 停止(阻塞)
  • Restart
  • 重新启动
  • Status
  • 状态

What isn't tested:

什么不是测试:

  • If reloading works properly
  • 如果重载工作正常
  • What happens if JBoss fails
  • 如果JBoss失败了怎么办?
  • What happens for several other possible fails
  • 其他几种可能发生的情况会失败

I didn't dive into several topics yet, especially:

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

  • What does set +e / set -e do exactly and is it required (it's used in other scripts)
  • set +e / set -e到底做什么?它是必需的吗?
  • What are the semantics of the log_*_msg functions and where should those be used
  • log_*_msg函数的语义是什么,应该在哪里使用
  • How to properly do the logging stuff (passing the location to JBoss)
  • 如何正确地完成日志工作(将位置传递给JBoss)
  • If the jboss-cli.sh script could and should be used for more things (like startup/running detection)
  • 如果jboss-cli。sh脚本可以并且应该用于更多的事情(如启动/运行检测)
  • If it's really necessary to have standalone.sh run as a process all the time
  • 如果有必要的话。sh始终作为一个进程运行
  • If the return values are appropriate
  • 如果返回值是合适的

Note that I'm definitely no shell script guru - I'm rather quite clueless. So if you find any stupidisms in the code or got any ideas for improvements, I'd be glad to hear them.

请注意,我绝对不是shell脚本大师——我很笨。所以,如果你在代码中发现了任何愚蠢的地方,或者有任何改进的想法,我很乐意听到。

#3


4  

Recently I wrote installer for JBoss AS 7 that downloads tar.gz file from RedHat's server, extract it, add jboss-as as service and makes some very basic configuration. With it I get ready for use JBoss AS 7 in several seconds.

最近,我为JBoss编写了一个安装程序,它可以下载tar文件。来自RedHat服务器的gz文件,提取它,添加jboss-as作为服务,并进行一些非常基本的配置。有了它,我可以在几秒钟内将JBoss用作7。

Installation script:

安装脚本:

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

Init script:

Init脚本:

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

I described the script steps in my blog post. It also has the link to download this script files as archive.

我在博客中描述了脚本步骤。它还具有将该脚本文件作为存档下载的链接。

#4


2  

#! /bin/sh

start(){
    echo "Starting JBoss 7"
    sudo -u jboss sh /usr/local/jboss/bin/standalone.sh
}

stop(){
    echo "Stopping JBoss 7"
    sudo -u jboss sh /usr/local/jboss/bin/jboss-admin.sh --connect command=:shutdown
}

restart(){
    stop
    # give stuff some time to stop before we restart
    sleep 60
    # protect against any services that can't stop before we restart 
    su -l jboss -c 'killall java'
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: jboss {start|stop|restart}"
        exit 1
    ;;
esac

exit 0

#5


2  

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

在bin目录下查看您的init.d/jbo -as独立。sh(jboss-as-7.1.0.CR1b)

#6


2  

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.

这里标为正确的答案对我不起作用。在重新启动时,您将得到与sudo使用相关的安全错误,并声明“对不起,您必须有一个tty来运行sudo。”进一步的研究表明,禁用sudo tty限制可能会导致密码的纯文本暴露,所以这不是好事。

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

#7


2  

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;

今晚我一直在努力,我想分享我的发现。最后,我按照这里的安装说明:http://ptoconnor.wordpress.com/2012/11/19/jboss-as-7-1- on- ubuntu-12-04-aws-instance-running-oracle-java-7/进行了一些修改;

I installed to /usr/share not /opt

我安装到/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

为了让jboss作为一个服务运行,我创建了一个符号链接到redhat/centos友好的/usr/share/jbo -as-7.1. final /bin/init.d/jboss-as独立。sh提供了7.1.1最终版本

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

然后做一些改动,使它更适合ubuntu

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

现在只需要使用上面的脚本安装jboss服务。

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

我知道现在有很多变化,但希望这将有助于下一个人搜索。我想要的只是一个JBPM主机……

#8


1  

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:

这里是我的gentoo。虽然还不完美,但很干净,对我来说工作得很好。第一个对jboss安装的小更改是:

~ # 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

我不能让启动说[OK],只要所有的部署都完成。我已经尝试了一些东西,但还没有运气——它要么永远等待,要么只是在shell脚本被分叉后说[OK]。停止是更好的,只要你设置的时间足够长。对数旋转很容易添加

#9


0  

There is a directory in the jboss distribution located in bin/init.d with a shell script you can place in init.d to launch jboss as a service. The script is called jboss-as-standalone.sh

jboss发行版中有一个目录位于bin/init中。使用shell脚本可以在init中放置。将jboss作为一项服务发布。这个脚本被称为jbo -as standard - alone.sh

#10


0  

I found no solution here which were really working... (at least for Ubuntu 12.04 LTS)

我没有找到真正有效的解决方案。(至少适用于Ubuntu 12.04 LTS)

So I made my own one (which is able to start JBoss & wait for it to complete, and stop it, using the start-stop-daemon utils) :

所以我自己做了一个(它可以启动JBoss并等待它完成,然后停止,使用start-stop-daemon utils):

#!/bin/bash
#
# JBoss standalone control script
#
# Based on the file provided in JBoss AS 7.1.1 (https://github.com/jbossas/jboss-as/blob/master/build/src/main/resources/bin/init.d/jboss-as-standalone.sh)
# inspired by http://*.com/questions/6880902/start-jboss-7-as-a-service-on-linux and http://ptoconnor.wordpress.com/2012/11/19/jboss-as-7-1-1-on-an-ubuntu-12-04-aws-instance-running-oracle-java-7/
# Modified for Ubuntu Server 12.04 by Anthony O.
#
# chkconfig: - 80 20
# description: JBoss AS Standalone
# config: /etc/default/jboss-as-7
#
### 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
#

JBOSS_CONF="/opt/jboss-as-7/bin/init.d/jboss-as.conf"

NAME=jboss-as-7
DEFAULT=/etc/default/$NAME

# Source function library.
. /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
  JBOSS_CONF=$DEFAULT
fi

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

# Set defaults.

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

if [ -z "$JBOSS_USER" ]; then
  JBOSS_USER="jboss"
fi
export JBOSS_USER

if [ -z "$JBOSS_PIDFILE" ]; then
  JBOSS_PIDFILE=/var/run/$NAME/jboss-as-standalone.pid
fi
export JBOSS_PIDFILE

if [ -z "$JBOSS_CONSOLE_LOG" ]; then
  JBOSS_CONSOLE_LOG=/var/log/$NAME/console.log
fi

# 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

MATCHING_ARGS=(--user "$JBOSS_USER" --pidfile "$JBOSS_PIDFILE")

start() {
  log_daemon_msg "Starting $NAME"
  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

  mkdir -p $(dirname $JBOSS_CONSOLE_LOG)
  cat /dev/null > $JBOSS_CONSOLE_LOG
  chown $JBOSS_USER $JBOSS_CONSOLE_LOG

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

  if [ ! -z "$JBOSS_USER" ]; then
    start-stop-daemon --start ${MATCHING_ARGS[@]} --oknodo --chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --retry $STARTUP_WAIT $(if [ "$LAUNCH_JBOSS_IN_BACKGROUND" == "true" ] ; then echo "--background" ; fi) --startas /bin/bash -- -c "exec $JBOSS_SCRIPT -c $JBOSS_CONFIG 2>&1 > $JBOSS_CONSOLE_LOG"
  else
    log_failure_msg "Error: Environment variable JBOSS_USER not set or empty."
    log_end_msg 1
    exit 1
  fi

  if [ "$LAUNCH_JBOSS_IN_BACKGROUND" == "true" ] ; then
    count=0
    launched_status=1

    until [ $count -gt $STARTUP_WAIT ]
    do
      grep 'JBAS015874:' $JBOSS_CONSOLE_LOG > /dev/null 
      if [ $? -eq 0 ] ; then
        launched_status=0
        break
      fi 
      sleep 1
      let count=$count+1;
    done

    log_end_msg $launched_status
    return $launched_status
  else
    log_end_msg $?
    return $?
  fi
}

stop() {
  log_daemon_msg "Stopping $NAME"

  END_STATUS=0
  if [ -f $JBOSS_PIDFILE ]; then
    start-stop-daemon --stop ${MATCHING_ARGS[@]} --retry $SHUTDOWN_WAIT
    END_STATUS=$?
    rm -f $JBOSS_PIDFILE
  fi
  log_end_msg $END_STATUS
  return $END_STATUS
}

status() {
  start-stop-daemon --status --verbose ${MATCHING_ARGS[@]}
  exit $?
}

reload() {
  log_begin_msg "Reloading $prog ..."
  start-stop-daemon --start --quiet --chuid ${JBOSS_USER} --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

One can change the JBOSS_CONF variable near the top of the file as well as removing it if the jboss-as.conf file is located at /etc/default/jboss-as-7.

可以更改文件顶部附近的JBOSS_CONF变量,也可以删除jboss-as。conf文件位于/etc/default/jboss-as-7。

#11


0  

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

要打印执行的命令的PID,请使用shell变量$!此变量将打印执行的进程的PID。

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
    ;;

#12


0  

Another way to run JBoss as a service on linux:

在linux上运行JBoss作为服务的另一种方式:

JBoss as service in linux

JBoss作为linux中的服务

#1


58  

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

在四处窥探了几个小时之后,我最终创建了/etc/ initt.com。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

Here's the content of java.sh:

以下是java.sh的内容:

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

And jboss.sh:

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

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

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

然后运行sudo update-rc。d jboss默认设置,以便jboss自动启动系统启动。

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

我发现这篇文章对创建上面的启动脚本很有帮助。同样,上面的脚本是针对Ubuntu的(在我的例子中是10.04版),所以在Fedora/RedHat或CentOS中使用它可能不会成功(在注释中所做的设置是不同的)。

#2


12  

I also took a shot at a script for Ubuntu 10.04 LTS. JBoss version is 7.1.1. I wanted a script that really tests for successful JBoss startup and that is able to shut down JBoss relatively gracefully. My starting point was the JBoss script included in the bin/init.d directory in the JBoss 7.1.1 download. I adapted this with some bits from other scripts on this page as well as other Ubuntu init scripts. Note that Ubuntu uses dash as its default init-script interpreter, not bash as apparently expected by the JBoss script. Of medium importance is that the logging takes place in /var/log/jboss-as instead of ${JBOSS_HOME}/standalone/log. The log is also used to determine if JBoss started up successfully.

我也尝试了一下Ubuntu 10.04 LTS的脚本。安装7.1.1 JBoss版本。我想要一个真正测试成功的JBoss启动的脚本,它能够相对优雅地关闭JBoss。我的出发点是bin/init中包含的JBoss脚本。在JBoss 7.1.1下载目录。我在这个页面上使用了其他脚本的一些片段,以及其他Ubuntu初始化脚本。注意,Ubuntu使用了dash作为默认的脚本解释器,而不是JBoss脚本所期待的bash。其中重要的一点是,日志记录发生在/var/log/jboss—而不是${JBOSS_HOME}/独立/日志。日志还用于确定JBoss是否成功启动。

jboss-as-standalone.sh

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

And the accompanying configuration (put it in /etc/default/jboss-as ):

以及相应的配置(将其放入/etc/default/jboss-as中):

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



What works (commands, use sudo service jboss-as <command> after linking the script from /etc/init.d/jboss-as):

什么是有效的(命令,使用sudo服务jbo -as ,链接来自/etc/init. dc /jboss-as的脚本):

  • Startup (blocking until the server started successfully)
  • 启动(阻塞,直到服务器成功启动)
  • Stopping (also blocking)
  • 停止(阻塞)
  • Restart
  • 重新启动
  • Status
  • 状态

What isn't tested:

什么不是测试:

  • If reloading works properly
  • 如果重载工作正常
  • What happens if JBoss fails
  • 如果JBoss失败了怎么办?
  • What happens for several other possible fails
  • 其他几种可能发生的情况会失败

I didn't dive into several topics yet, especially:

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

  • What does set +e / set -e do exactly and is it required (it's used in other scripts)
  • set +e / set -e到底做什么?它是必需的吗?
  • What are the semantics of the log_*_msg functions and where should those be used
  • log_*_msg函数的语义是什么,应该在哪里使用
  • How to properly do the logging stuff (passing the location to JBoss)
  • 如何正确地完成日志工作(将位置传递给JBoss)
  • If the jboss-cli.sh script could and should be used for more things (like startup/running detection)
  • 如果jboss-cli。sh脚本可以并且应该用于更多的事情(如启动/运行检测)
  • If it's really necessary to have standalone.sh run as a process all the time
  • 如果有必要的话。sh始终作为一个进程运行
  • If the return values are appropriate
  • 如果返回值是合适的

Note that I'm definitely no shell script guru - I'm rather quite clueless. So if you find any stupidisms in the code or got any ideas for improvements, I'd be glad to hear them.

请注意,我绝对不是shell脚本大师——我很笨。所以,如果你在代码中发现了任何愚蠢的地方,或者有任何改进的想法,我很乐意听到。

#3


4  

Recently I wrote installer for JBoss AS 7 that downloads tar.gz file from RedHat's server, extract it, add jboss-as as service and makes some very basic configuration. With it I get ready for use JBoss AS 7 in several seconds.

最近,我为JBoss编写了一个安装程序,它可以下载tar文件。来自RedHat服务器的gz文件,提取它,添加jboss-as作为服务,并进行一些非常基本的配置。有了它,我可以在几秒钟内将JBoss用作7。

Installation script:

安装脚本:

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

Init script:

Init脚本:

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

I described the script steps in my blog post. It also has the link to download this script files as archive.

我在博客中描述了脚本步骤。它还具有将该脚本文件作为存档下载的链接。

#4


2  

#! /bin/sh

start(){
    echo "Starting JBoss 7"
    sudo -u jboss sh /usr/local/jboss/bin/standalone.sh
}

stop(){
    echo "Stopping JBoss 7"
    sudo -u jboss sh /usr/local/jboss/bin/jboss-admin.sh --connect command=:shutdown
}

restart(){
    stop
    # give stuff some time to stop before we restart
    sleep 60
    # protect against any services that can't stop before we restart 
    su -l jboss -c 'killall java'
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: jboss {start|stop|restart}"
        exit 1
    ;;
esac

exit 0

#5


2  

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

在bin目录下查看您的init.d/jbo -as独立。sh(jboss-as-7.1.0.CR1b)

#6


2  

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.

这里标为正确的答案对我不起作用。在重新启动时,您将得到与sudo使用相关的安全错误,并声明“对不起,您必须有一个tty来运行sudo。”进一步的研究表明,禁用sudo tty限制可能会导致密码的纯文本暴露,所以这不是好事。

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

#7


2  

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;

今晚我一直在努力,我想分享我的发现。最后,我按照这里的安装说明:http://ptoconnor.wordpress.com/2012/11/19/jboss-as-7-1- on- ubuntu-12-04-aws-instance-running-oracle-java-7/进行了一些修改;

I installed to /usr/share not /opt

我安装到/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

为了让jboss作为一个服务运行,我创建了一个符号链接到redhat/centos友好的/usr/share/jbo -as-7.1. final /bin/init.d/jboss-as独立。sh提供了7.1.1最终版本

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

然后做一些改动,使它更适合ubuntu

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

现在只需要使用上面的脚本安装jboss服务。

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

我知道现在有很多变化,但希望这将有助于下一个人搜索。我想要的只是一个JBPM主机……

#8


1  

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:

这里是我的gentoo。虽然还不完美,但很干净,对我来说工作得很好。第一个对jboss安装的小更改是:

~ # 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

我不能让启动说[OK],只要所有的部署都完成。我已经尝试了一些东西,但还没有运气——它要么永远等待,要么只是在shell脚本被分叉后说[OK]。停止是更好的,只要你设置的时间足够长。对数旋转很容易添加

#9


0  

There is a directory in the jboss distribution located in bin/init.d with a shell script you can place in init.d to launch jboss as a service. The script is called jboss-as-standalone.sh

jboss发行版中有一个目录位于bin/init中。使用shell脚本可以在init中放置。将jboss作为一项服务发布。这个脚本被称为jbo -as standard - alone.sh

#10


0  

I found no solution here which were really working... (at least for Ubuntu 12.04 LTS)

我没有找到真正有效的解决方案。(至少适用于Ubuntu 12.04 LTS)

So I made my own one (which is able to start JBoss & wait for it to complete, and stop it, using the start-stop-daemon utils) :

所以我自己做了一个(它可以启动JBoss并等待它完成,然后停止,使用start-stop-daemon utils):

#!/bin/bash
#
# JBoss standalone control script
#
# Based on the file provided in JBoss AS 7.1.1 (https://github.com/jbossas/jboss-as/blob/master/build/src/main/resources/bin/init.d/jboss-as-standalone.sh)
# inspired by http://*.com/questions/6880902/start-jboss-7-as-a-service-on-linux and http://ptoconnor.wordpress.com/2012/11/19/jboss-as-7-1-1-on-an-ubuntu-12-04-aws-instance-running-oracle-java-7/
# Modified for Ubuntu Server 12.04 by Anthony O.
#
# chkconfig: - 80 20
# description: JBoss AS Standalone
# config: /etc/default/jboss-as-7
#
### 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
#

JBOSS_CONF="/opt/jboss-as-7/bin/init.d/jboss-as.conf"

NAME=jboss-as-7
DEFAULT=/etc/default/$NAME

# Source function library.
. /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
  JBOSS_CONF=$DEFAULT
fi

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

# Set defaults.

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

if [ -z "$JBOSS_USER" ]; then
  JBOSS_USER="jboss"
fi
export JBOSS_USER

if [ -z "$JBOSS_PIDFILE" ]; then
  JBOSS_PIDFILE=/var/run/$NAME/jboss-as-standalone.pid
fi
export JBOSS_PIDFILE

if [ -z "$JBOSS_CONSOLE_LOG" ]; then
  JBOSS_CONSOLE_LOG=/var/log/$NAME/console.log
fi

# 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

MATCHING_ARGS=(--user "$JBOSS_USER" --pidfile "$JBOSS_PIDFILE")

start() {
  log_daemon_msg "Starting $NAME"
  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

  mkdir -p $(dirname $JBOSS_CONSOLE_LOG)
  cat /dev/null > $JBOSS_CONSOLE_LOG
  chown $JBOSS_USER $JBOSS_CONSOLE_LOG

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

  if [ ! -z "$JBOSS_USER" ]; then
    start-stop-daemon --start ${MATCHING_ARGS[@]} --oknodo --chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --retry $STARTUP_WAIT $(if [ "$LAUNCH_JBOSS_IN_BACKGROUND" == "true" ] ; then echo "--background" ; fi) --startas /bin/bash -- -c "exec $JBOSS_SCRIPT -c $JBOSS_CONFIG 2>&1 > $JBOSS_CONSOLE_LOG"
  else
    log_failure_msg "Error: Environment variable JBOSS_USER not set or empty."
    log_end_msg 1
    exit 1
  fi

  if [ "$LAUNCH_JBOSS_IN_BACKGROUND" == "true" ] ; then
    count=0
    launched_status=1

    until [ $count -gt $STARTUP_WAIT ]
    do
      grep 'JBAS015874:' $JBOSS_CONSOLE_LOG > /dev/null 
      if [ $? -eq 0 ] ; then
        launched_status=0
        break
      fi 
      sleep 1
      let count=$count+1;
    done

    log_end_msg $launched_status
    return $launched_status
  else
    log_end_msg $?
    return $?
  fi
}

stop() {
  log_daemon_msg "Stopping $NAME"

  END_STATUS=0
  if [ -f $JBOSS_PIDFILE ]; then
    start-stop-daemon --stop ${MATCHING_ARGS[@]} --retry $SHUTDOWN_WAIT
    END_STATUS=$?
    rm -f $JBOSS_PIDFILE
  fi
  log_end_msg $END_STATUS
  return $END_STATUS
}

status() {
  start-stop-daemon --status --verbose ${MATCHING_ARGS[@]}
  exit $?
}

reload() {
  log_begin_msg "Reloading $prog ..."
  start-stop-daemon --start --quiet --chuid ${JBOSS_USER} --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

One can change the JBOSS_CONF variable near the top of the file as well as removing it if the jboss-as.conf file is located at /etc/default/jboss-as-7.

可以更改文件顶部附近的JBOSS_CONF变量,也可以删除jboss-as。conf文件位于/etc/default/jboss-as-7。

#11


0  

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

要打印执行的命令的PID,请使用shell变量$!此变量将打印执行的进程的PID。

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
    ;;

#12


0  

Another way to run JBoss as a service on linux:

在linux上运行JBoss作为服务的另一种方式:

JBoss as service in linux

JBoss作为linux中的服务