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 tomcatAnd that's all she wrote.


5 Comments:
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!
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
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
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.
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
Post a Comment
Links to this post:
Create a Link
<< Home