Redhat RHEL tomcat init script
This is my effort to create a nice clean init script to start and stop tomcat for Redhat or RHEL servers.
Two file need to be created to get the init script working
- /etc/sysconfig/tomcat
- /etc/init.d/tomcat
/etc/sysconfig/tomcat
# Environment variables needed for tomcat startup # I have setup the following links # /usr/lib/jvm/jdk to point to the present installation of java/jdk # /opt/tomcat to point to /opt/apache-tomcat-version-no: PATH=$PATH:/usr/lib/jvm/jdk/bin JAVA_HOME="/usr/lib/jvm/jdk" CATALINA_HOME="/opt/tomcat" CATALINA_BASE="/opt/tomcat"/etc/init.d/tomcat
#!/bin/bash # # tomcat # # chkconfig: 345 84 15 # description: Start up the Tomcat servlet engine. # config: /etc/sysconfig/tomcat # processname: tomcat # pidfile: /var/run/tomcat.pid # Source function library. . /etc/init.d/functions # Loading the configuration parameters. if [ -f /etc/sysconfig/tomcat ]; then . /etc/sysconfig/tomcat fi RETVAL=0 case "$1" in start) if [ -f $CATALINA_HOME/bin/startup.sh ]; then logger -s "Starting Tomcat" /bin/su -l tomcat -c $CATALINA_HOME/bin/startup.sh RETVAL=$? [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat fi ;; stop) if [ -f $CATALINA_HOME/bin/shutdown.sh ]; then logger -s "Stopping Tomcat" /bin/su -l tomcat -c $CATALINA_HOME/bin/shutdown.sh RETVAL=$? [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat fi ;; restart) $0 stop $0 start ;; version) if [ -f $CATALINA_HOME/bin/version.sh ]; then logger -s "Display Tomcat Version" /bin/su -l tomcat -c $CATALINA_HOME/bin/version.sh RETVAL=$? fi ;; *) echo $"Usage: $0 {start|stop|restart|version}" exit 1 ;; esac exit $RETVALInstalling
Once the above files have been created issue the command
chmod 755 /etc/init.d/tomcat chkconfig --add tomcat chkconfig --list tomcatThis will create the necessary links in the /etc/rcn.d directories. Lets now try and start and shutdown of tomcat.
service tomcat start service tomcat stop service tomcat stopYou can inspect the log file /var/log/messages. You should have log file entries there identifying the user who restarted tomcat
eg:May 21 14:02:07 server1 bob : Stopping Tomcat May 21 14:02:09 server1 bob : Starting Tomcat May 21 14:05:34 server1 bob : Stopping Tomcat May 21 14:06:14 server1 bob : Stopping Tomcat May 21 14:06:42 server1 last message repeated 2 times May 21 14:07:19 server1 bob : Starting TomcatBackground
Ok Now finer details. The above script was tested with RHEL 5.5, should work with Centos and Fedora distributions as well. What runlevels does it start at and in which sequence. This is controlled by the below line in /etc/init.d/tomcat
# chkconfig: 345 84 15The above line indicates tomcat should be started up in runlevels 3, 4, and 5. That is what 345 in the above line signifies
Further more 84 and 15. 84 indicates the sequence number in which it needs to be started up. 15 indicates the sequence number in which it needs to be stopped.
The reasoning for choosing 84 for startup sequence. The default start sequence number for Apache is 85. Knowing that I thought it would be most appropriate to give a sequence number of 84 so that tomcat would be started before Apache.
Environment Variable Used
PATH=$PATH:/usr/lib/jvm/jdk/bin
I have /usr/lib/jvm/jdk as a symlink to the java installation folderJAVA_HOME="/usr/lib/jvm/jdk"
I have /usr/lib/jvm/jdk as a symlink to the java installation folderCATALINA_HOME="/opt/tomcat"
CATALINA_BASE="/opt/tomcat"Assumption is the tomcat tar file for tomcat is extracted to lets say /opt/apache-tomcat-6.0.26. I further have a symlink /opt/tomcat pointing to /opt/apache-tomcat-6.0.26. Hence why I have used /opt/tomcat for the catalina_home and _base environment variables. If you prefer using /usr/local/tomcat feel free to adjust accordingly
The symlinks allow you to upgrade java and tomcat, and not having to worry about your init scripts or your environment variables.
Once you have upgraded java and tomcat all you need to do is update your symlinks to point to the directory where you new version has been installedFuture Improvement
My wish list
- Incorporate a mechanism to be able to check the tomcat "status".
- Possibly incorporate a PID file.
Once the PID file has been incorporated, it might be easier to get the status checking added on to the init script.
Acknowledgements
The above was inspired from http://www.raibledesigns.com/tomcat/boot-howto.html Thank you.
I have taken the original script from the above link, extended it so it is slightly clearer, and I don't have to manually create the