Automatic Startup

Once you have Tomcat installed and running, you can set it to start automatically when your system reboots. This will ensure that every time your system comes up, Tomcat will be running and handling requests. Unix users will make changes to their init scripts, and Windows users will need to set Tomcat up as a service. Both approaches are outlined in this section.

Automatic Startup on Linux

If you've installed Tomcat via an RPM package, getting it to run on a reboot is just a matter of telling your system to run the tomcat or tomcat55 service (depending on which RPM package you installed) when it enters a multiuser run level.

Tip

If you know how to use chkconfig, as the root user you can simply chkconfig tomcat on for the run level(s) of your choice.

Use the chkconfig command to make the tomcat service start in the run level(s) of your choice. Here's an example of how to make it start in run levels 2, 3, 4, and 5:

# chkconfig --level 2345 tomcat on

Tip

If chkconfig does not see the tomcat service, try tomcat55 instead (the JPackage.org RPM package's init script has this name). Otherwise, you probably did not install Tomcat as an RPM package. Below, we show how to add a simple init script to make it work anyway.

Now, query your configuration to make sure that startup is actually set:

# chkconfig --list tomcat
tomcat    0:off   1:off   2:on   3:on    4:on    5:on    6:off

Now, reboot and see if Tomcat starts up when the system comes back up.

If you didn't use the RPM package to install Tomcat, you can still set up Tomcat to start on reboots. Tomcat does not come with a Linux init script, but it is simple to create one that would just start Tomcat at boot time and stop it on shutdown.-Example 1-2 is a very simple Tomcat init script for Linux.

Example 1-2. A Tomcat init script for Linux

#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.

JAVA_HOME=/usr/java/jdk1.6.0_02
CATALINA_HOME=/opt/apache-tomcat-6.0.14
export JAVA_HOME CATALINA_HOME

exec $CATALINA_HOME/bin/catalina.sh $*

Save this script in a file named tomcat and change the file ownership and group to root, and then chmod it to 755:

# chown root.root tomcat
# chmod 755 tomcat

Copy the script to the /etc/rc.d/init.d directory after modifying the JAVA_HOME and CATALINA_HOME environment variables to fit your system. Then, set the new tomcat service to start and stop automatically by using chkconfig, as shown earlier in this section.

Automatic Startup on Solaris

If you have installed Tomcat via a Blastwave Solaris CSW package, your Tomcat has been preconfigured to start at boot time. You do not have to do anything extra to make it work.

If not, you'll need to create yourself a simple init script, as shown for Linux in the previous section; it should work fine. Save it to /etc/init.d/tomcat and set the permissions like this:

# chmod 755 /etc/init.d/tomcat
# chown root /etc/init.d/tomcat
# chgrp sys /etc/init.d/tomcat

Set the new tomcat service to start and stop automatically by symbolically linking it into the /etc/rc3.d directory (as the root user):

# ln -s /etc/init.d/tomcat /etc/rc3.d/S63tomcat
# ln -s /etc/init.d/tomcat /etc/rc3.d/K37tomcat

The numbers S63 and K37 may be varied according to what other startup scripts you have; the S number controls the startup sequence and the K number controls the shutdown (kill) sequence. The system startup program init invokes all files matching /etc/rc3.d/S* with the parameter start as part of normal system startup, and start is just the right parameter for catalina.sh. The init program also invokes each script file named rc3.d/K* with the parameter stop when the system is being shut down.

Automatic Startup on Windows

Under Windows, Tomcat can be run as a Windows service. Although you can use this to start and stop the server, the most common reason for creating a Tomcat service is to ensure that it is started each time your machine boots up.

Your first task is to find the Services control panel. On a standard Windows install, this requires accessing several menus: Start Menu → Programs → Administrative Tools → Services. Alternately, you can go Start Menu → Settings → Control Panel, and then double-click on Administrative Tools, and again on Services. Once you have the Services control panel, locate the entry for Apache Tomcat (the entries are normally in alphabetical order), and double-click on it, as shown in Figure 1-7.

Automatic startup under Windows

Figure 1-7. Automatic startup under Windows

In the Apache Tomcat Properties dialog box, you should ensure that the startup type is set to Automatic rather than Manual, which will cause Tomcat to start up whenever your machine reboots.

Automatic Startup on Mac OS X

Mac OS X, like most other operating systems, uses system init scripts to allow you to start, stop, and restart services automatically just as you would on a Linux system via /etc/rc.d/init.d or via BSD's /etc/init.d. In Mac OS X Tiger (10.4), Apple has introduced a new central system-wide controller called launchd.[5] launchd gives you more flexibility over how services are controlled and who can access these services. It provides a very simple property list (plist) configuration file that allows you to set up what daemon runs and how the daemon is accessed. Due to the differences[6] in behavior between how launchd expects the daemon it has launched to react and how the Tomcat scripts operate, we have to create a shell script that won't fork or have the parent process exit to overcome this problem.

Let's create the script for usage in the tomcat.plist and put it in the Tomcat installation binary directory (both the following shell script and the .plist file are included in the book's examples; you may download them from http://www.oreilly.com/catalog/9780596101060):

$ vi /usr/local/tomcat/bin/tomcat-launchd.sh
#!/bin/bash
# Shell script to launch a process that doesn't quit after launching the JVM
# This is required to interact with launchd correctly.

function shutdown(  )
{
        $CATALINA_HOME/bin/catalina.sh stop
}

export CATALINA_HOME=/usr/local/tomcat
export TOMCAT_JVM_PID=/tmp/$$

. $CATALINA_HOME/bin/catalina.sh start

# Wait here until we receive a signal that tells Tomcat to stop..
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP

wait `cat $TOMCAT_JVM_PID`

Next, we need to create the launchd property list file for Tomcat. Load up your favorite text editor and edit tomcat.plist:

$ vi tomcat.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>EnvironmentVariables</key>
        <dict>
                <key>CATALINA_HOME</key><string>/usr/local/tomcat</string>
                <key>JAVA_HOME</key><string>/System/Library/
Frameworks/JavaVM.framework/Home</string>
        </dict>
        <key>Label</key><string>org.apache.tomcat</string>
        <key>OnDemand</key><false/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/tomcat/bin/tomcat-launchd.sh</string>
        </array>
        <key>RunAtLoad</key><true/>
        <key>ServiceDescription</key><string>Apache Tomcat</string>
        <key>StandardErrorPath</key><string>usr/local/tomcat/logs/
launchd.stderr</string>
        <key>StandardOutPath</key><string>usr/local/tomcat/logs/
launchd.stdout</string>
        <key>UserName</key><string>nobody</string>
</dict>
</plist>

Now that we have the configuration file, we need to place it in the correct location so launchd can access it. To ensure the script is executed even if no users are logged in, the script should be placed in /Library/LaunchDaemons:

$ sudo cp tomcat.plist /Library/LaunchDaemons

Another requirement of launchd is that both the daemon and property list file need to be owned by the root user and the daemon needs to be executable. Let's ensure that the correct ownership and executable flag is set on these files:

$ chown root:wheel tomcat-launchd.sh
$ chmod +x tomcat-launchd.sh
$ chown root:wheel /Library/LaunchDaemons/tomcat.plist

Our final step in this process is to load the script into launchd:

$ sudo launchctl load /Library/LaunchDaemons/tomcat.plist

You can ensure your plist has been loaded by running the following command:

$ sudo launchctl list
com.apple.dashboard.advisory.fetch
com.apple.dnbobserverd
com.apple.KernelEventAgent
com.apple.mDNSResponder
com.apple.nibindd
com.apple.periodic-daily
com.apple.periodic-monthly
com.apple.periodic-weekly
com.apple.portmap
com.apple.syslogd
com.vix.cron
org.samba.nmbd
org.postfix.master
org.xinetd.xinetd
org.samba.smbd
org.apache.tomcat

Tip

Notice that Tomcat is now running via launchd (org.apache.tomcat) this is the Label you specified in the property list file above.

If for some reason it hasn't loaded, ensure that all your paths are correct, the files have the correct permissions and are otherwise accessible.

Automatic Startup on FreeBSD

If you installed the FreeBSD port of Tomcat 6, this section shows the standard way of configuring Tomcat to start at boot time and stop on a shutdown.

To enable Tomcat to start on a reboot and be shut down gracefully as part of the shutdown sequence, you need to put a controller script in the /usr/local/etc/rc.d/ directory. The controller script's filename must end in .sh, and it must be executable (see "man rc").

The FreeBSD port of Tomcat 6 comes with an RCng script that you can use for starting, stopping, and restarting the server. This script is /usr/local/etc/rc.d/tomcat60.sh.

Make sure you have added this line to your /etc/rc.conf file:

tomcat60_enable="YES

This is what enables Tomcat 6 to start at boot time. Once you have done that and you reboot, Tomcat should start. It should also be able to shut down gracefully when you shut down your computer.



[5] * You can find a detailed overview on Apple's support page related to this great new service: http://developer.apple.com/macosx/launchd.html

[6] launchd expects the service to be started and run until signaled, whereas the scripts for Tomcat (catalina.sh) launch the Tomcat JVM and then quit. This is a mismatch that the tomcat-launchd.sh script fixes.

Get Tomcat: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.