Starting Tomcat as a Service on Linux

来源:互联网 发布:推销产品 下载什么软件 编辑:程序博客网 时间:2024/05/21 17:34
Source: http://www.spaceprogram.com/knowledge/2004/01/starting-tomcat-as-service-on-linux.html

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 ___FCKpd___0quot;Starting Tomcat service: "
#daemon -c
$startup
RETVAL=$?
echo
}

stop(){
action ___FCKpd___0quot;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 ___FCKpd___0quot;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.

5 Comments:

At Thursday, March 17, 2005 2:32:00 PM, Slappy said...

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!

 
At Friday, June 03, 2005 12:59:00 PM, rahennig said...

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

 
At Thursday, February 15, 2007 4:07:00 AM, Timok said...

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

 
At Wednesday, March 07, 2007 4:38:00 PM, Alex S said...

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.

 
At Sunday, March 11, 2007 1:11:00 PM, Krisen said...

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

原创粉丝点击