Friday, January 09, 2004

Starting Tomcat as a Service on Linux

Introduction

This document will teach you how to setup Tomcat to run as a service (startup when booted) on Linux.
Intended Audience
System admins.
Instructions
This is actually pretty easy and will be presented step by step.

1. Save tomcat start / stop script

Copy and paste the following script into your text editor:

# This is the init script for starting up the
# Jakarta Tomcat server
#
# chkconfig: 345 91 10
# description: Starts and stops the Tomcat daemon.
#

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

tomcat=/usr/local/jakarta-tomcat
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/local/jdk

start(){
echo -n $"Starting Tomcat service: "
#daemon -c
$startup
RETVAL=$?
echo
}

stop(){
action $"Stopping Tomcat service: " $shutdown
RETVAL=$?
echo
}

restart(){
stop
start
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
# This doesn't work ;)
status tomcat
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac

exit 0

Edit the lines that start with tomcat and export to match where you installed
tomcat and your jdk.

Note: I can't remember where I first got the original version of this script
so if you deserve credit for this, let us know.

2. Save to /etc/init.d and chmod

Save the edited file above to /etc/init.d directory as "tomcat" (at
least on most newer releases since /etc/init.d is a standard now). Then
you have to allow execute access to the script, so run:

chmod a+x tomcat

3. Add to appropriate run level directories

The easy way to do this is to just simply run:

chkconfig --add tomcat

And that's all she wrote.