PDA

View Full Version : Install jboss as daemon (autostart at boot)


CuBone
December 28th, 2007, 08:25 PM
Introduction

This guide will let you run jboss as a daemon, that means you'll have a jboss instance that starts a computer boot. When you download jboss you get some example init scripts for different distributions but none for ubuntu. I did however get it to work with minor changes to the red hat init script. This is how I did it, there might be some better way and if you find better solutions than the ones in this guide please let me now and I'll post them here.

At the end of this tutorial you will have (if everything goes as planned) a jboss instance running that starts at computer boot with the help of a daemon script in /etc/init.d/. To keep things tidy jboss will have a separate user called jboss as process owner.

Ubuntu versions tested

Ubuntu 7.10 32 bit Server Version
Ubuntu 7.10 32 bit Desktop Version

Step 1: Install suns jdk

There have been problems reported with jboss and java 6 in the past. I don't know if there still is compatibility issues but to be safe I use java 5.


sudo apt-get install sun-java5-jdk



Step 2: create the jboss user

The reason to use a separate user account for jboss is to control the permissions of the jboss instance. You don't want jboss running as root with unlimited access to the whole system. There is no password created for the jboss user, and you probably don't need one either (unless you actually want to login as the jboss user).


sudo mkdir /home/jboss
sudo useradd -s /bin/bash -d /home/jboss jboss
sudo chown jboss:jboss /home/jboss/


Step 3: Download jboss

Go to http://labs.jboss.com/jbossas/downloads/ and get the latest stable version of jboss (4.2.2.GA when this guide was written). Download it to your home directory.
Now you have a file in ~/jboss-4.2.2.GA.zip

I used unzip to extract the archive, to install it run
sudo apt-get install unzip
but feel free to extract it with what ever program you feel fit.

sidenote:
When I installed jboss on my server I installed it in the /opt/folder instead of /home/jboss/. Where ever you decide to put jboss make sure the jboss user has read and write permission to the jboss folder and it's subfiles/-folders.

Move it to jboss home and extract it. To make a future upgrade of jboss a little easier a symlinc to jboss home is created. When you decide to upgrade jboss you'll just have to edit the symlinc to point to the new version.


cd ~
sudo mv jboss-4.2.2.GA.zip /home/jboss/jboss-4.2.2.GA.zip
sudo chown jboss:jboss /home/jboss/jboss-4.2.2.GA.zip
sudo su jboss
cd ~
unzip jboss-4.2.2.GA.zip
ln -s jboss-4.2.2.GA jboss
exit


Step 5:Get the init scriptt

All the changes to the red hat init script is in the setup section in the beginning of the script. If you followed my guide to the letter you can download my version of the script thats attached to this post. If you installed java or jboss in other directories it's probably easier to edit the script your self.

Option 1: download my script

Download the scriptattached to this post to your home folder then run this to move it to the correct folder.


sudo mv ~/jboss/jboss_init.sh /etc/init.d/jboss


End option 1

Option 2: edit the original red hat script yourself.

Copy the red hat init script to the init.d directory and rename it.


sudo cp /home/jboss/jboss/bin/jboss_init_redhat.sh /etc/init.d/jboss


This is all the changes I made to the scipt, nothing fancy, just som variables for jboss that needs to be set correct.


#define where jboss is - this is the directory containing directories log, bin, conf etc

JBOSS_HOME=${JBOSS_HOME:-"/home/jboss/jboss"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user

JBOSS_USER=${JBOSS_USER:-"jboss"}

#make sure java is in your path

JAVAPTH=${JAVAPTH:-"/usr/lib/jvm/java-1.5.0-sun"}

#configuration to use, usually one of 'minimal', 'default', 'all'

JBOSS_CONF=${JBOSS_CONF:-"default"}

#the host where jboss should answer. o.o.o.o means answer all calls. set this to yourhost.com

JBOSS_HOST=${JBOSS_HOST:-"0.0.0.0"}

# Uncomment this line to store the console output, otherwise it's sent to /dev/null
(good for debugging)
# JBOSS_CONSOLE=${JBOSS_CONSOLE:-"$JBOSS_HOME/server/$JBOSS_CONF/log/console.log"}



End option 2

Step 6: Install the init script

Make your script owned by root and executable.
Create run level shortcuts for the script.


sudo chown root:root /etc/init.d/jboss
sudo chmod ug+x /etc/init.d/jboss
sudo update-rc.d jboss defaults


To start jboss you type


sudo /etc/init.d/jboss start


If everything has gone as planned you should now have a functional installation of jboss that will start itself on computer boot.

Step 7: Redirecting traffic from port 80 to jboss port 8080 (Optional)

The default port for jboss is 8080 but the standard port for http traffic is 80. Port 80 is however a restricted port and can only be bound by root. To get around this little pickle you can redirect the incoming traffic on port 80 to 8080, the same goes for the https traffic on port 443 to jboss 8443. Here's a way to do that.


iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443
iptables -t nat -A PREROUTING -p udp -m udp --dport 443 -j REDIRECT --to-ports 8443


This forward script was originally found here, http://www.2nrds.com/port-forwarding-in-linux

Remove
If you found a better way to do this or if you just don't want this installed anymore here's what you do.

Warning! All files will be removed from your sytem.

Remove the init daemon script

sudo /etc/init.d/jboss stop
sudo update-rc.d jboss remove
sudo rm /etc/init.d/jboss


Remove jboss files and user

sudo userdel jboss
sudo rm -r /home/jboss


Remove java

sudo apt-get remove sun-java5-jdk



Remove unzip

sudo apt-get remove unzip



End

That's all for me. This is my first ubuntu guide and my knowledge about linux is a bit limited. So if you think I made some bad decisions in this setup please let me know and we can make this guide better together.

codelion
January 2nd, 2008, 09:46 PM
Has to be plural instead of singular:

sudo update-rc.d jboss defaults

codelion
January 2nd, 2008, 11:44 PM
Also, for desired results I'm using a minus sign in

JBOSS_HOST=${JBOSS_HOST:-"0.0.0.0"}

codelion
January 2nd, 2008, 11:51 PM
And for creating the user in order for it to work I had to create a group first, and I used these two lines instead of the three lines

sudo groupadd jboss
sudo useradd -s /bin/bash -d /home/jboss -m -g jboss jboss

No need for mkdir or chown. The chown needed the group to exist, but here the -m option takes care of the mkdir and chown.

CuBone
January 3rd, 2008, 09:25 PM
And for creating the user in order for it to work I had to create a group first, and I used these two lines instead of the three lines

sudo groupadd jboss
sudo useradd -s /bin/bash -d /home/jboss -m -g jboss jboss

No need for mkdir or chown. The chown needed the group to exist, but here the -m option takes care of the mkdir and chown.

Here my lack of linux experience shows. I'll look in too this and to your other posts to. Thank you for your input

mark_k
January 4th, 2008, 10:55 AM
(1) Word to the wise, I created the 'jboss' user via the ubuntu gui widget (before I found this post) and set the default shell to '/bin/false'. Don't do that. Use the 'useradd' command above that specifies a login shell, or set it with the gui widget to a real shell, otherwise the user can't run java. Rookie mistake, but it threw me for a bit of a loop.

(2) I had to make the same +/- change for JBOSS_CONSOLE. It now looks like this

# Uncomment this line to store the console output, otherwise it's sent to /dev/null
# JBOSS_CONSOLE=${JBOSS_CONSOLE:-"$JBOSS_HOME/server/$JBOSS_CONF/log/console.log"}

Thanks to everybody involved in putting this script together.


- Mark

drapsag
March 14th, 2008, 11:56 AM
there is an error in your start/stop script

it must be:
JBOSS_BIND_ADDR=${JBOSS_BIND_ADDR:-"-b $JBOSS_HOST"}

medined
July 19th, 2008, 12:03 PM
Thanks for the post.

AdagioParaCuerdas
July 25th, 2008, 05:11 PM
i got an error with the sh script

# sudo /etc/init.d/jboss start
JBOSS_CMD_START = cd /home/jboss/jboss/bin; /home/jboss/jboss/bin/run.sh -c default
/etc/init.d/jboss: 71: Syntax error: "(" unexpected

it refers to:


function procrunning() {
procid=0
JBOSSSCRIPT=$(echo $JBOSSSH | awk '{print $1}' | sed 's/\//\\\//g')
for procid in `/sbin/pidof -x "$JBOSSSCRIPT"`; do
ps -fp $procid | grep "${JBOSSSH% *}" > /dev/null && pid=$procid
done
}


stop() {
pid=0
procrunning

johog
August 6th, 2008, 02:04 PM
Tanks.
I'm running Ubuntu 8.04
Installed java-6 and jboss-5.0.0.CR1
Editing of /etc/init.d/jboss become little different for me.

#define where jboss is - this is the directory containing directories log, bin, conf etc
#JBOSS_HOME=${JBOSS_HOME:-"/usr/local/jboss"} JOHOG
JBOSS_HOME=${JBOSS_HOME:-"/home/jboss/jboss"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"jboss"}

#make sure java is in your path
#JAVAPTH=${JAVAPTH:-"/usr/local/jdk/bin"} JOHOG
JAVAPTH=${JAVAPTH:-"/usr/lib/jvm/java-6-sun-1.6.0.06"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"default"}

#the host where jboss should answer. o.o.o.o means answer all calls. JOHOG
JBOSS_HOST=${JBOSS_HOST:-"0.0.0.0"}

alekeri
August 30th, 2008, 12:19 PM
Thanks for this guide. It's for me is the only way to start JBoss as daemon on Ubuntu.
I tried to use JS Wrapper as alternative way, but unfortunately my attempts was unsuccessful due to "wrapper: cannot execute binary file" error. May be somebody has experience with mentioned tool?

brightonmike
September 9th, 2008, 05:44 AM
i got an error with the sh script

# sudo /etc/init.d/jboss start
JBOSS_CMD_START = cd /home/jboss/jboss/bin; /home/jboss/jboss/bin/run.sh -c default
/etc/init.d/jboss: 71: Syntax error: "(" unexpected

it refers to:
I came unstuck with this problem in the redhat script, something to do with the 'function' keyword? - Not enough shell skills!

I found an alternative script on the forum at:
http://ubuntuforums.org/archive/index.php/t-492697.html

which seems to work...

Thanks for the post and replies by the way..

octavio@javafact.com
September 14th, 2008, 02:13 PM
i got an error with the sh script

# sudo /etc/init.d/jboss start
JBOSS_CMD_START = cd /home/jboss/jboss/bin; /home/jboss/jboss/bin/run.sh -c default
/etc/init.d/jboss: 71: Syntax error: "(" unexpected

it refers to:

According to "Portable Shell Programming" by Bruce Blinn, a function is not delcared with a "function" keyword. A function is declared with only the function name. Delete the mistaken keyword from the script and it will execute.

cristian.vulpe
September 19th, 2008, 05:14 PM
Great information!
If I'd have knew that I wouldn't have spent the time to dig it for myself. However, if somebody is interested in my experience, I have installed JBoss 4.2.3.GA on my Ubuntu (8.04) machine and when trying to run it as a service I had some problems with the already-available scripts (e.g. for RedHat, SuSE, HPUX). Therefore I have derived the RedHat script and here are the steps that made my installation work:

1. Create in /etc/init.d folder a file for the JBoss service (e.g. jboss.sh). Note that you need the right to create files there...
2. Using your preferred editor (e.g. gedit), fill in the file with the following content (note that changes for your local installation are needed):

#!/bin/sh
#
# This script has been derived from the following one:
# $Id: jboss_init_redhat.sh 71252 2008-03-25 17:52:00Z dbhole $
#
# JBoss Control Script
#
# To use this script run it as root - it will switch to the specified user
#
# Here is a little (and extremely primitive) startup/shutdown script
# for Ubuntu systems. It assumes that JBoss lives in /usr/local/jboss,
# it's run by user 'jboss' and JDK binaries are in /usr/local/jdk.
# All this can be changed in the script itself.
#
# Either modify this script for your requirements or just ensure that
# the following variables are set correctly before calling the script.
#
# Major changes from the original one:
# * usage of the shutdown.sh script instead of killing the jboss' JVM process. This simplifies the structure of the script.
# * added support for bind address change (see the JBOSS_HOST variable).

#define the JAVA_HOME
JAVA_HOME=${JAVA_HOME:-"/usr/local/jdk"}

#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/usr/local/jboss"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"jboss"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"default"}

#bind address
JBOSS_HOST=${JBOSS_HOST:-"127.0.0.1"}

#define the script used to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF -b $JBOSS_HOST"}

#define the script used to stop jboss
JBOSSST=${JBOSSST:-"$JBOSS_HOME/bin/shutdown.sh -S -s jnp://$JBOSS_HOST:1099"}

if [ "$JBOSS_USER" = "RUNASIS" ]; then
SUBIT=""
else
SUBIT="su - $JBOSS_USER -c "
fi

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then
# ensure the file exists
touch $JBOSS_CONSOLE
if [ ! -z "$SUBIT" ]; then
chown $JBOSS_USER $JBOSS_CONSOLE
fi
fi

if [ -n "$JBOSS_CONSOLE" -a ! -f "$JBOSS_CONSOLE" ]; then
echo "WARNING: location for saving console log invalid: $JBOSS_CONSOLE"
echo "WARNING: ignoring it and using /dev/null"
JBOSS_CONSOLE="/dev/null"
fi

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/dev/null"}

JBOSS_CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"
JBOSS_CMD_STOP="cd $JBOSS_HOME/bin; $JBOSSST"

if [ ! -d "$JBOSS_HOME" ]; then
echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
exit 1
fi

case "$1" in
start)
cd $JBOSS_HOME/bin
echo JBOSS_CMD_START = $JBOSS_CMD_START
if [ -z "$SUBIT" ]; then
eval $JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &
else
$SUBIT "$JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &"
fi
;;
stop)
cd $JBOSS_HOME/bin
echo JBOSS_CMD_STOP = $JBOSS_CMD_STOP
if [ -z "$SUBIT" ]; then
eval $JBOSS_CMD_STOP >${JBOSS_CONSOLE} 2>&1 &
else
$SUBIT "$JBOSS_CMD_STOP >${JBOSS_CONSOLE} 2>&1 &"
fi
;;
restart)
$0 stop
# there must be a time between the "stop" and the "start" because the mentioned two are asynchronous operations.
# this depends on how fast your machine is.
sleep 3
$0 start
;;
*)
echo "usage: $0 (start|stop|restart)"
esac

3. Configure the script to start for your preferred runlevels (e.g. 3 and 5). To do this you can use the sysvconfig utility.

The same information is available at http://cristivulpe.blogspot.com/2008/09/jboss-on-ubuntu-as-system-v-service.html.

Regards,
cristi

sPyTe
November 24th, 2008, 07:38 PM
Thank you very much!! It seems works for me!!

Just in case, thank you very much ^^!!!

csaxena
November 25th, 2008, 06:13 AM
Hello All,

I am trying to RUN JBOSS As Daemon on Ubuntu Command Line Server.
I have jboss-4.2.1.GA Server.
After doing all the necessary set for running jboss as Daemon. I am getting an error saying "Command Not Found". "sudo /etc/init.d/jboss start"

& when i tried this command "sudo /etc/init.d/jboss.sh start" it says "sudo: unable to execute /etc/init.d/jboss.sh: No such file or directory"

Please help me out with this.

-Chetan.