Thursday, July 26, 2012

How to make a tomcat as a service in CENTOS/RHCE


Introduction

This document will teach you how to setup Tomcat to run as a service (startup when booted) on Linux. 



Step 1:
# touch /etc/rc.d/init.d/tomcat
# groupadd tomcat
# useradd -g tomcat tomcat
# chown -R tomcat.tomcat /usr/local/tomcat6
# chmod a+x /etc/rc.d/init.d/tomcat
# vim /etc/rc.d/init.d/tomcat

Step 2:
Copy the below content in the above mentioned file and edit the path what you need correct

#!/bin/sh
#
#
#
# chkconfig: 345 80 20
# description: Tomcat is the Apache Servlet Engine
# processname: tomcat
# pidfile: /var/run/tomcat.pid
#
# Tomcat name :)
TOMCAT_PROG=tomcat

# if TOMCAT_USER is not set, use tomcat like Apache HTTP server
if [ -z "$TOMCAT_USER" ]; then
 TOMCAT_USER="tomcat"
fi
  
RETVAL=0
  
# start and stop functions
start() {
    echo -n "Starting tomcat: "
  
    chown -R $TOMCAT_USER:$TOMCAT_USER /usr/local/
 jakarta -tomcat/*   
    chown -R $TOMCAT_USER:$TOMCAT_USER /home/tomcat/*
    su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/startup.sh'
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
    return $RETVAL
}
  
stop() {
    echo -n "Stopping tomcat: "
    su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/shutdown.sh'
    RETVAL=$?
    Echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/tomcat.pid   
    rm -rf /usr/local/
 jakarta -tomcat/work/*
}
  
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 2
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac
  
exit $RETVAL
 
Step 3: 
Finally type the below command 

# chkconfig --add tomcat
# chkconfig tomcat on
# service tomcat restart

0 comments:

Powered by Blogger.