Wednesday, April 15, 2009

Installing Apache Tomcat 5 as a service on Linux5(CentOS5)

1. First, create the shell script file below and name it tomcat under /etc/init.d/

#!/bin/sh
#
# /etc/init.d/tomcat
#
# chkconfig: 345 84 16
#
# This is the init script for starting up the
# Jakarta Tomcat server
#
# description: Starts and stops the Tomcat daemon.
#

tomcat=/usr/local/tomcat
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/local/java
RETVAL=0

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

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

restart() {
stop
start
}

status() {
SHUTDOWN_PORT=`netstat -vatn |grep LISTEN | grep 8080 |wc -l`
if [ $SHUTDOWN_PORT -eq 0 ]; then
echo "Tomcat stopped"
else
echo "Tomcat is running!"
fi

}

# Handle the different input options
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit 0
2. Change the Permissions:

chmod u+x /etc/init.d/tomcat

3. You can test the script by running this command
cd /etc/init.d
./tomcat status
which will return the Tomcat process info if it is running; otherwise, it will return blank

4. Add tomcat as a service by running the following:
chkconfig --add tomcat

5. Now the tomcat is a service, you can issue these commands from anywhere:

service tomcat start
service tomcat stop
service tomcat restart
service tomcat status
6. Check whether the tomcat will start at boot:
chkconfig --list tomcat

No comments:

Post a Comment