Startup script for Tomcat on Centos | Redhat | Fedora
General
Author: Sebastian Hennebrueder
Source code: http://www.laliluna.de/download/tomcat-linux-startup-script.zip
PDF version of the tutorial: http://www.laliluna.de/download/tomcat-linux-startup-script-en.pdf
Beginning
Why not using the catalina.sh startup.sh and shutdown.sh?
Normal script behaviour
The PID is a convenient handle for the service. You can show the status of the process for example
cat "/proc/$(cat /var/run/tomcat.pid)/status"
Or you can kill a process brutally
kill -9 $(cat /var/run/tomcat.pid)
Sample
We call init 1 to switch to single user mode in Linux.
Linux assumes that the service is running and will call /etc/init.d/tomcatd stop
It is very important that the name of the lock file corresponds to the script name: tomcatd
Further behaviour
If you check how other scripts work, you can see the following behaviour:
If a service has a pidfile and is running, a call with 'start' is ignored.
After the service was started successfully a script returns 0 else something greater than 0.
Scripts
tomcatRunner
It executes the command as background process and writes the PID to the PID file.
Init script
Things you might change
killproc -p $pidfile -d 6 $servicename
Tomcat cluster
You might use the variable servicename to specify the CATALINA_BASE directory.
CATALINA_BASE="$CATALINA_HOME/$servicename"
Further readings
/usr/share/doc/initscripts-8.45.19.EL/sysvinitfiles - Introduction on writing scripts
http://wrapper.tanukisoftware.org a service wrapper which allows to restart Tomcat automatically in case of out of of memory errors
http://tille.garrels.be/training/bash/ - a very helpful guide when writing bash scripts
http://commons.apache.org/daemon/ and http://tomcat.apache.org/tomcat-6.0-doc/setup.html in case you want to start Tomcat as unprivileged user using privileged ports, for example port 80
Do you need expert help or consulting? Get it at http://www.laliluna.de
Seminars and Education at reasonable prices on a wide range of Java Technologies, Design Patterns, and Enterprise Best Practices Improve your development quality
In-depth, detailed and easy-to-follow Tutorials for JSP, JavaServer Faces, Struts, Spring, Hibernate and EJB
An hour of support can save you a lot of time - Code and Design Reviews to insure that the best practices are being followed! Reduce solving and testing time
Consulting on Java technologies Get to know best suitable libraries and technologies