Okay, here's the "production ready" init.d script I'll be using on my
Ubuntu 8.04 server. It could be very easily tweaked for other Linux
distros. Basically, I started with the /etc/init.d/skeleton "template"
script as a model and just added a hook to manually kill the Tomcat
process (PID) if the stop doesn't end the process as expected.
FYI, I've located my Tomcat home (latest binary distro extracted)
directory at /opt/tomcat6. Also, I'm running the daemon as an
unprivileged "tomcat6" user, so you'll want to create that group and
user first ("sudo groupadd tomcat6 && sudo useradd -g tomcat6
tomcat6").
After creating the following script as /etc/init.d/tomcat6 you can
also automatically configure this to automatically start and stop at
the proper times by issuing the following command (make sure ending
dot/period remains):
sudo update-rc.d tomcat6 start 92 2 3 4 5 . stop 08 0 1 6 .
#!/bin/sh
### BEGIN INIT INFO
# Provides: tomcat6
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Tomcat.
# Description: Start the Tomcat servlet engine.
### END INIT INFO
# Author: Jamie Krug <
ja...@thekrugs.com>
# Written: May 4, 2009
PATH=/usr/sbin:/usr/bin:/sbin:/bin
NAME=tomcat6
DESC="Tomcat servlet engine"
TOMCAT_HOME=/opt/$NAME
PIDFILE=$TOMCAT_HOME/work/$NAME.pid
DAEMON=$TOMCAT_HOME/bin/catalina.sh
TOMCAT_USER=$NAME
JAVA_HOME=/usr
export JAVA_HOME
# This allows the Tomcat startup script to create a PID file:
export CATALINA_PID="$PIDFILE"
# Read configuration variable file if it is present
#[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
if start-stop-daemon --test --start --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
su $TOMCAT_USER $DAEMON start
sleep 5
if start-stop-daemon --test --start --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
log_end_msg 1
else
log_end_msg 0
fi
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
if start-stop-daemon --test --start --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
log_progress_msg "(not running)"
else
su $TOMCAT_USER $DAEMON stop
sleep 5
if start-stop-daemon --test --start --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
log_progress_msg "(stop succeeded, no need to kill PID.)"
else
log_progress_msg "(stop failed, forcing kill of PID `cat
$PIDFILE`)"
kill -9 `cat $PIDFILE`
fi
fi
rm -f $PIDFILE
log_end_msg 0
;;
restart|force-reload)
if start-stop-daemon --test --stop --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
$0 stop
sleep 1
fi
$0 start
;;
status)
if start-stop-daemon --test --start --pidfile "$PIDFILE" \
--user $TOMCAT_USER --startas "$JAVA_HOME/bin/java" \
>/dev/null; then
if [ -f "$PIDFILE" ]; then
log_success_msg "$DESC is not running, but pid file exists."
exit 1
else
log_success_msg "$DESC is not running."
exit 3
fi
else
log_success_msg "$DESC is running with pid `cat $PIDFILE`"
fi
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}"
exit 1
;;
esac
exit 0
Best,
Jamie
On May 1, 3:24 pm, Jamie Krug <
jamiek...@gmail.com> wrote:
> @Barney,
>
> Thanks, great stuff. I'm loving the AJP proxy stuff that I gathered from
> your comment on Sean's blog (and subsequent post). It took a little more
> learning in the URL rewriting world, but I know have some preexisting
> (fairly complex) rewrite rules working in conjunction with a rewrite/proxy
> (AJP) to Tomcat/Railo for one app. I like the idea of simply unzipping the
> distro folder for multiple instances, and it's good to just know that
> someone else is doing it that way :)
>
> Thanks again,
> Jamie
>
> On Fri, May 1, 2009 at 2:44 PM, Barney Boisvert <
bboisv...@gmail.com> wrote:
>
> > Just unzip the binary distro to a folder, and symlink to catalina.sh
> > from /etc/init.d/tomcat_XXX. Have to add the chkconfig comment lines
> > to the top of the script, but that's it. I don't do the shared
> > binaries (splitting CATALINA_HOME and CATALINA_BASE) because disk
> > space is cheap, and I want to be able to upgrade separately. Just
> > unzip a new copy of the distro for each instance, obviously tweaking
> > server.xml to avoid port collisions and such. Then I front them all
> > with Apache (using mod_proxy) to stich them together into a single
> > unified webroot (along with some JRun-managed paths).
>
> > cheers,
> > barneyb
>
> > On Fri, May 1, 2009 at 11:40 AM, Jamie Krug <
jamiek...@gmail.com> wrote:
> > > @Barney,
>
> > > Out of curiosity, how do you have Tomcat setup on your CentOS server? Is
> > it
> > > a CentOS-provided package, or are you manually configuring your own init
> > > script and just grabbing the latest Tomcat .tar.gz from the Apache site?
> > I
> > > think I recall you have multiple Tomcat instances running too? Are those
> > > sharing a single Tomcat library and just starting as separate services,
> > or
> > > just multiple copies of the Tomcat download?
>
> > --
> > Barney Boisvert
> >
bboisv...@gmail.com
> >
http://www.barneyb.com/