Startup script for Tomcat on Centos | Redhat | Fedora
PDF version of the tutorial: http://www.laliluna.de/download/tomcat-linux-startup-script-en.pdf
Apache Tomcat provides already scripts to start it up. They are powerful and correct but I wanted to have a script, I can understand. The scripts provided by Apache Tomcat have workarounds for IBM400, Cygwin and MacOs. After simplifying the script I arrived at about 185 lines versus 780.�
A script is called by the linux init process with parameters like start or stop. If start is passed as argument, the script should start up the service, note the PID (process ID) somewhere in /var/run/... and should create a lock file in /var/lock/subsys/
If a script is called with the 'stop' parameter, it shuts the service down identified by the PID in the PID file and removes the PID file and the lock file. We have the default behaviour defined so let us look at some border cases.�
We will need two scripts. The first one executes a command as unprivileged user and notes the PID in the pid file. The second is a normal startup script. We have to split the code into two files as the execution of Java programs does not provide a out of the box mechanism to start with root and then to downgrade privileges. You can do this with the 'commons daemon', see the link in further readings. �
But as we are not using it, we use the bash command runuser which executes a subshell under the specified user. Our main shell script has no mean to get the PID of a subshell background process. Therefore the command passed to runuser must note the PID. As a consequence we cannot call java directly with runuser but need a simple script which is called from runuser and calls Java.and notes the PID in the pid file.
This approach has a limitation as well. The script starting Tomcat is already running as unprivileged user. As a consequence Tomcat cannot listen to a privileged port which are ports below 1024. You cannot have Tomcat listen on port 80 for example. Either start Tomcat as root, which I would not recommend or have a look at the Tanuki service wrapper or alternatively the commons daemon. See further readings below.�
If you have multiple Tomcats you normally reuse the same Tomcat installation for all Tomcats. In this case the CATALINA_HOME points to the installation whereas CATALINA_BASE points to the configuration and webapps folder of one instance.�
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
The tutorial is provided as is. I do not give any warranty or guaranty any fitness for a particular purpose. In no event shall I be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this tutorial, even if I has been advised of the possibility of such damage.