Start JBoss 7 as a service on Linux

2019-01-20 23:33发布

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?

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

12条回答
做自己的国王
2楼-- · 2019-01-20 23:45

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
查看更多
走好不送
3楼-- · 2019-01-20 23:49

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.

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:

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

查看更多
Lonely孤独者°
4楼-- · 2019-01-20 23:53

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

查看更多
甜甜的少女心
5楼-- · 2019-01-20 23:54

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

查看更多
Explosion°爆炸
6楼-- · 2019-01-20 23:55

Another way to run JBoss as a service on linux:

JBoss as service in linux

查看更多
SAY GOODBYE
7楼-- · 2019-01-21 00:00

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

查看更多
登录 后发表回答