[ start | index | login ]
start > knowledgebase > linux > java > Running Tomcat infinitely via Daemontools

Running Tomcat infinitely via Daemontools

Created by paolo. Last edited by paolo, one year and 157 days ago. Viewed 1,871 times. #8
[diff] [history] [edit] [rdf]
labels
attachments
daemontools is a collection of tools for managing UNIX services written by the mathematician and celebrated crytographer Daniel J. Bernstein. The most prominent tools in the daemontools collection are:

a) supervise. This tool monitors a service. It starts the service and restarts the service if it dies. Setting up a new service is easy: all supervise needs is a directory with a run script that runs the service.

b) multilog. This tool saves error messages to one or more logs. It optionally timestamps each line and, for each log, includes or excludes lines matching specified patterns. It automatically rotates logs to limit the amount of disk space used. If the disk fills up, it pauses and tries again, without losing any data.

With daemontools it is possible to circumvent the limitation of the init program to allow a program to run 'forever', as daemontools will always ensure that the program runs a service. To install daemontools, just download the source from >>http://cr.yp.to/daemontools/install.html.

Take note that daemontools will NOT compile with GCC 3.x and above without doing this hack:

  • once you've extracted the source, edit src/error.h and replace the line "extern int errno;" with "#include <errno.h>"
  • run package/install to continue compiling and installation of daemontools in /service.
As Tomcat is normally run in the background when you use init, we will need to tweak it a bit to make it run in the foreground for daemontools to properly access it. Create a directory that's safe for the daemontools 'supervise' command to write on (e.g. /usr/local/tomcat/tomcat)

Create an executable shell script /usr/local/tomcat/tomcat/run with the following contents:

#!/bin/sh
export JAVA_HOME=/usr/java/jdk
cd /usr/local/tomcat/bin
exec 2>&1
exec setuidgid tomcat ./catalina.sh run

To enable logging, create a log directory in the directory, e.g. /usr/local/tomcat/tomcat/log with an run script that goes as follows:

#!/bin/sh
exec setuidgid tomcat multilog t ./main

This will allow the multilog program to write logrotated logs to /usr/local/tomcat/tomcat/log/main. Create the said directory and have the tomcat user have read/write/execute access to it.

Once done, have /usr/local/tomcat symlinked to /service, e.g.

# ln -s /usr/local/tomcat/tomcat /service/tomcat

Tomcat should run within 5 seconds. To inspect if Tomcat is indeed running under daemontools, use the svstat tool like this:

# svstat /service/tomcat
/service/tomcat: up (pid 26146) 165 seconds

To issue a HUP signal on Tomcat and have the supervise utility automatically restart Tomcat:

# svc -t /service/tomcat

To stop Tomcat:

# svc -d /service/tomcat

To start Tomcat (after issuing a -d, not needed when -t is passed):

# svc -u /service/tomcat

To check the logs, just take a look at the 'current' file in the logs directory, e.g.:

# tail -f /usr/local/tomcat/tomcat/log/main/current

If you're running Tomcat automatically at startup via init.d scripts, please disable such feature by running:

# chkconfig --del tomcat

More information about daemontools can be found in >>http://cr.yp.to/daemontools.html

Original instructions from Will Hartung, with fixes from Jamon Camisso.

no comments | post comment
Powered by snipsnap.org Found a mistake in a howto? Let us know via an email to p.blikibugs at rimuhosting com.