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.

12 comments:

  1. Here's one way to get a status.. add this function:

    status(){
    numproc=`ps -ef | grep catalina | grep -v "grep catalina" | wc -l`
    if [ $numproc -gt 0 ]; then
    echo "Tomcat is running..."
    else
    echo "Tomcat is stopped..."
    fi
    }

    Then call that in your case statement when "status" is used. Hope that helps!

    ReplyDelete
  2. This seems to work a bit better for me I have found (running on RHEL3 WS):
    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

    ReplyDelete
  3. Hello,
    The problem with this is that Tomcat is then running as root, isn't it?
    How shall we make it run as another user?
    Thanks,
    Timok

    ReplyDelete
  4. To change it so tomcat runs as another user, change the $startup line to:

    su - $user -c "$startup"

    and $shutdown line to:

    action $"Stopping Tomcat service: " su - $user "$shutdown"

    And of course, define user up at the top where the paths are defined. Voila.

    ReplyDelete
  5. tomcat5 sets default service in /etc/rc.d/init.d directory, however it does not start automatically. running the chkconfig line starts the service automatically.

    Thanks for your help!
    Kris

    ReplyDelete
  6. slappy's function doesn't perform well and in any case it shows Tomcat as running. It needs to be modified as below

    if [ $numproc -ne 0 ]; then

    ReplyDelete
  7. Im facing some issue with status function:
    While running tomcat as a service, in the status field:

    /etc/init.d/tomcat: line10: ./etc/sysconfig/network. Permission denied.
    /etc/init.d/tomcat: line13:[=no]: command not found.
    /etc/init.d/tomcat: line55: status: command not found

    ReplyDelete
  8. heres what i managed to write, and which works for me...
    # Tomcat auto-start
    #
    # description: auto-starts tomcat service
    # processname: tomcat
    # pidfile: /var/run/tomcat.pid

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

    startt() {
    echo "Starting Tomcat services..."
    su - tomcat -c "sh $startup"
    }

    stopp() {
    echo "Stopping Tomcat services..."
    su - tomcat -c "sh $shutdwn"
    }

    statuss() {
    numproc=`ps -ef|grep catalina|grep -v "grep catalina"|wc -l`
    if [ $numproc -ne 0 ]; then
    echo "Tomcat is running..."
    else
    echo "Tomcat is stopped..."
    fi
    }

    case "$1" in
    start)
    startt
    ;;
    stop)
    stopp
    ;;
    status)
    statuss
    ;;
    restart)
    stopp
    startt
    ;;
    *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
    esac
    exit 0

    ReplyDelete
  9. of course, what i managed to write, was mostly from this blog and its comments. So, thanks to you all...

    ReplyDelete
  10. This is not working properly in centos...

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete