Sunday, December 19, 2004

Running Tomcat as a Service on Linux

This is the script that im using to startup and shutdown tomcat automatically on Linux

1. Save tomcat start / stop script
Copy and paste the following script into your text editor:




#!/bin/bash
#
# Startup script for Tomcat
#
# chkconfig: 345 84 16
# description: Tomcat jakarta JSP server

TOMCAT_HOME=/usr/local/tomcat
startup=$TOMCAT_HOME/bin/startup.sh
shutdown=$TOMCAT_HOME/bin/shutdown.sh

#Necessary environment variables
export JAVA_HOME="/usr/local/java/java"
export CATALINA_HOME="/usr/local/tomcat"
#export LD_KERNEL_ASSUME="2.2.5"

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

# Source networking configuration.
. /etc/sysconfig/network

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

#Check for tomcat script
if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]
then
echo "Tomcat not available..."
exit
fi

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

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


status() {
ps ax --width=1000 grep "[o]rg.apache.catalina.startup.Bootstrap start" awk '{printf $1 " "}' wc awk '{print $2}' > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt if [ $line -gt 0 ]; then echo -n "tomcatd ( pid " ps ax --width=1000 grep "[o]rg.apache.catalina.startup.Bootstrap start" awk '{printf $1 " "}' echo -n ") is running..." echo else echo "Tomcat is stopped" fi } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 3 start ;; status) status ;; *) echo "Usage: tomcatd {start stop restart status}" exit 1 esac exit 0 Edit the lines that start with tomcat and export to match where you installed tomcat and your jdk. 2. Save to /etc/init.d and chmod
Save the edited file above to /etc/init.d directory as "tomcatd"

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

thats it.

this will also allow you to start and stop tomcat manually

service tomcatd start
service tomcatd stop

No comments:

Post a Comment