Tuesday, June 26, 2007

Configuring Tomcat to autostart on Linux OS

I spent the whole of this morning trying to get tomcat to autostart in my Ubuntu Linux box, so I thought i would document it here for others to benefit from it!

To begin with -

1) create a system user tomcat5
useradd tomcat5 -r --system

2) Copy the startup script given below to your /etc/init.d directory and name the scipt as tomcat5 (remember to change script paramater)

3) use update-rc.d to create the symlinks
update-rc.d tomcat5 defaults 90

Incase you make some errors while at the above command (like i did!) use the following command to remove the tomcat service
update-rc.d -f tomcat5 remove and then rerun step 3

4) Finally, do a
chown tomcat5 -R to make sure tomcat can read everything

-----Start Script-----

#! /bin/sh
#
# initscript
# This file should be used to construct scripts to be
# placed in /etc/init.d.
#
# Author: Gerard Sylvester .
#
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="servlet container"
NAME=tomcat5
CATALINA_HOME=/
CATALINA_BASE=/
DAEMON_START="${CATALINA_HOME}/bin/startup.sh"
DAEMON_STOP="${CATALINA_HOME}/bin/shutdown.sh"
DAEMON_CONTROL="${CATALINA_HOME}/bin/catalina.sh"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
TOMCAT_USER=tomcat5

export CATALINA_HOME CATALINA_BASE

# Gracefully exit if the package has been removed.
test \( -x $DAEMON_START -a -x $DAEMON_STOP -a -x $DAEMON_CONTROL \) || exit 0

# Read config file if it is present.
#if [ -r /etc/default/$NAME ]
#then
# . /etc/default/$NAME
#fi

#
# Function that starts the daemon/service.
#
d_start() {
#start-stop-daemon --start --quiet --pidfile $PIDFILE \
# --exec $DAEMON
su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_START"
}

#
# Function that stops the daemon/service.
#
d_stop() {
#start-stop-daemon --stop --quiet --pidfile $PIDFILE \
# --name $NAME
su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_STOP"
}

#
# Function that sends a SIGHUP to the daemon/service.
#
d_debug() {
#start-stop-daemon --stop --quiet --pidfile $PIDFILE \
# --name $NAME --signal 1
su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_CONTROL run"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
debug)
echo -n "starting $DESC in debug mode: $NAME"
d_debug
echo "done."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
# echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|debug}" >&2
exit 1
;;
esac

exit 0

---End Script----

1 comment:

Pritpalkaur said...

Its good to share information