Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Running Tomcat 5

So far, I've had you run Tomcat by typing catalina run. Some of you already familiar with Tomcat might wonder why I chose that method. As I pointed out before, I like it because if I have any startup errors, I get them in the same console window I used to type the command. For development, or for the initial setup of a Tomcat instance, it is good practice, but for production use, you generally choose another method.

Tomcat 5 bin files

Before I discuss the options, take a look at the contents of the bin directory. Figure 3.1 shows what's inside.

Figure 3.1Figure 3.1 Tomcat 5 bin directory contents.

Looks daunting, doesn't it? Well, if I group files together, it is not so bad.

First, there are a few jar files, or Java libraries, which Tomcat uses when it starts. They are described in Table 3.2.

Table 3.2 Tomcat Startup Jars

Jar

Description

bootstrap.jar

Contains the basic launcher class files for Tomcat.

commons-daemon.jar

A series of classes from the Jakarta Commons project for daemonizing a Java program.

commons-launcher.jar

A Java launcher program that is being split off from the Tomcat project and moved under Commons.

commons-logging-api.jar

An application programming interface (API) for logging that allows Tomcat to use any logging package which follows this API.


NOTE

If you are not familiar with the Jakarta Commons project, http://jakarta.apache.org/commons/, it is a project to create reusable Java components. There are a number of available components for beans, logging, connection pooling, collections, and so on. Many of these components owe their origins to Tomcat, so don't be surprised to see examples involving Tomcat. But the Tomcat developers found them to be useful enough to live in their own project space. Check them out! It might save you some valuable time on one of your projects.

Next, we can identify the main shell scripts, catalina.bat and catalina.sh, which we have already been using. They are called, in turn, by a few other scripts, shown in Table 3.3.

Table 3.3 Tomcat Startup Scripts

Script

Description

catalina.bat/.sh

Main Tomcat control script

shutdown.bat/.sh

A convenience wrapper that calls the main script with the stop command

startup.bat/.sh

A convenience wrapper that calls the main script with the start command


There are two Windows executables available for installing Tomcat as a service: tomcat.exe and tomcatw.exe. The only difference is that the latter runs a graphical version.

The launcher is a new concept in Tomcat 5. It used Apache Ant to start and stop Tomcat, as well as to invoke various Tomcat tools. This method uses the LauncherBootstrap.class, catalina.xml, launcher.properties, and *-using-launcher* files.

Table 3.4 describes a number of other files—utility scripts mostly—in the bin directory.

Table 3.4 Miscellaneous bin Files

File

Description

cpappend.bat

Windows shell script for building a CLASSPATH variable.

digest.bat/.sh

Script for digesting passwords (see next chapter, in the section "Digesting Passwords").

jsvc.tar.gz

A package for running Tomcat as a daemon (see Chapter 17).

setclasspath.bat/.sh

Script for building the CLASSPATH variable—called by catalina.bat/.sh.

tool-wrapper*

Scripts for running tools or other Java executables that need the same common class loader as Tomcat. Used, for example, to run the digest.bat/.sh script.


Tomcat 5 Run Options

Now I can talk about the options for running Tomcat. Basically, you have three: you can start Tomcat manually on the command line and run it as an application until you stop it, as you have been doing; you can start Tomcat as a daemon so that it starts automatically on machine boot; and you can start Tomcat with the Ant-based launcher. I discuss the latter in Chapter 17, "Administering Tomcat."

Running Tomcat 5 Manually

If you are happy starting Tomcat the way I showed earlier, you can continue to do so. But if you'd prefer the "standard" way, you can type the following on Unix:

startup.sh

Use the following on Windows:

startup

Note that startup simply invokes either catalina.bat or catalina.sh with the start argument.

Similarly, for stopping Tomcat, type the following on Unix:

shutdown.sh

Type this on Windows:

shutdown

In this case, the command invokes either catalina.bat or catalina.sh with the stop argument.

Running Tomcat 5 Automatically

In any production system, you want Tomcat to start when the machine boots and run as a daemon or unattended, system-owned process. On Unix, this means you'll typically create a startup script that the operating system (OS) will execute on the boot and shutdown of the machine. On Windows, you'll typically install Tomcat as a service for the same results.

The key to a daemon process is that the application can respond to signals from the OS and take appropriate startup or shutdown measures. In a Unix daemon, the application responds to signals sent by the OS. For a Windows daemon, or service, the application implements various methods that the OS makes calls to.

In fact, a Java process is not naturally a daemon: it is meant to be invoked by some client via the main method, which is the entry point to the application. To run a Java process as a daemon, you must provide a native wrapper around it that provides the proper interface to the OS so it can be run as a daemon. The Tomcat distribution comes with just such a wrapper, or daemonizer, from the Jakarta Commons Daemon project. This project actually has two different flavors: JSVC, the daemonizer for Unix, and procrun, for Windows. The latter comes by default with Tomcat, in the form of tomcat.exe and tomcatw.exe in the $CATALINA_HOME/bin directory. I'll save the discussion on JSVC for Chapter 17. For Unix, you also have the option to create a simple startup/shutdown script, which is what we'll do here.

Running Tomcat 5 Automatically on Unix

To run a process automatically on Unix, you usually create a startup script in a predefined directory that the OS can then find and execute as it starts up. As you probably know, Unix runs at various run or init levels. At each level, it starts various services. Although there are technically 10 possible levels, the ones that typically concern you are 1, which is when the system is running in single-user mode; 2, which is when the system is running in multi-user mode with Network File Service (NFS); 3, for full multi-user mode; and 5, which is full multi-user mode plus X Windows.

To tell Unix which services should start at which run level, a collection of directories under /etc/rc.d contains service startup scripts. Figure 3.2 shows a typical example (on a Linux server).

Figure 3.2Figure 3.2 Unix /etc/rc.d contents.

What you see is a directory for each Unix run level from 0 to 6, named rc[n].d, where [n] is the run level. The convention is to put the scripts in the init.d directory and then create symbolic links in the various run-level directories as appropriate. The convention also provides a way to determine the order of script execution at a particular run level: the symbolic links are named S[n][scriptname], where [n] is a two-digit number and [scriptname] is the actual name of the script in the /etc/rc.d/init.d directory that is being pointed to. There is a similar convention for stopping services, whereby Unix executes scripts as it descends through run levels. Scripts that need to execute to stop services are named K[n][scriptname], where [n] is a two-digit number and [scriptname] is the actual name of the script in the /etc/rc.d/init.d directory that is being pointed to. To make this work, you must configure the actual script to handle both start and stop situations, as you'll do in a minute.

You usually want Tomcat, or any Web server for that matter, to start at run level 3. I like to start Tomcat after any database server that my application depends on starts but before Apache starts (assuming that I am running Tomcat behind an Apache server). So I start by creating a script file called tomcat5 in /etc/rc.d/init.d that looks something like Listing 3.1.

Listing 3.1 Tomcat rc File

#!/bin/sh
#
# Startup script for Tomcat
#
 
case "$1" in
 start)
    echo -n "Starting Tomcat"
    JAVA_HOME="/usr/java/jdk1.3.1_08" ; export JAVA_HOME && 
/usr/tomcat5/bin/startup.sh
    ;;
 stop)
    echo -n "Stopping Tomcat"
    JAVA_HOME="/usr/java/jdk1.3.1_08" ; export JAVA_HOME && 
/usr/tomcat5/bin/shutdown.sh
    ;;
 restart)
    $0 stop
    $0 start
    ;;
 *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
 
exit 0

This script expects to be called with one of three arguments: start, stop, or restart. All I do for the startup is set the $JAVA_HOME environment variable, which Tomcat needs, and then call startup.sh in the bin directory of the Tomcat installation. And that is all there is to it.

After you create the script, you need to create a symbolic link for it in the appropriate rc directory. Because I usually use rc3.d for Tomcat, I type

cd /etc/rc.d/rc3.d
ln -f ../init.d/tomcat5 K90tomcat5
ln -f ../init.d/tomcat5 S90tomcat5

What you are doing here is providing a startup link, S99tomcat5, which the OS will then call with the start argument. Because you want to gracefully shut down Tomcat when the machine stops, you'll also provide a shutdown link so that the OS will call with the stop argument. Obviously, both links point to the same script, which you've already coded to handle both arguments. Note too that I made the sequence number 90 because I want Tomcat to be one of the last things to start at run level 3. I make sure that MySQL (or whatever database I'm using) has a lower sequence number so it starts first. I also have Apache start after Tomcat by using a higher sequence number.

In Chapter 17, we'll go into building and using JSVC from the Commons Daemon project so that Tomcat can run as a true daemon.

Running Tomcat 5 as a Service on Windows

There are two ways you can install Tomcat as a daemon on Windows. The easiest is to download and execute the Windows installation of Tomcat. When you do, Tomcat is automatically installed as a service. The second option is to manually execute tomcat.exe in $CATALINA_HOME/bin.

Using the Tomcat Installer for Windows

Go back to the Tomcat binary download page, http://jakarta.apache.org/site/binindex.cgi, find the Tomcat 5 section, and download the *.exe distribution. After you download it, double-click to start the installation. You'll be prompted for the install location (by default, c:\Program Files\Apache Software Foundation\Tomcat 5.0), Java location, and a few other things. Once it is installed, however, you have a service now available called Apache Tomcat.

Manually Installing Tomcat as a Service

First, make sure you set the %JAVA_HOME% and %CATALINA_HOME% system environment variables in Control Panel, System, Environment Variables. Then, run the script shown in Listing 3.2.

Listing 3.2 Tomcat 5 Service Install Script

%CATALINA_HOME%\bin\tomcat //IS//Tomcat5 --DisplayName "Tomcat5" \
--Description "Tomcat5" --ImagePath "%CATALINA_HOME%\bin\bootstrap.jar" \
--StartupClass org.apache.catalina.startup.Bootstrap;main;start \
--ShutdownClass org.apache.catalina.startup.Bootstrap;main;stop \
--Java "%JAVA_HOME%\jre\bin\client\jvm.dll" \
--JavaOptions -Xrs \
--StdErrorFile "%CATALINA_HOME%\logs\stderr.log" \
--StdOutputFile "%CATALINA_HOME%\logs\stdout.log"

CAUTION

This script is JDK 1.4.2-specific. For JDK 1.3.x, you need to specify a different DLL in the --Java option, like this: --Java "%JAVA_HOME%\jre\bin\classic\jvm.dll" \.

When you type this command, put everything on one line; I show line breaks only for readability. What you are doing here is telling Windows to install Tomcat as the service named Tomcat5. This service will run tomcat.exe, which is really just procrun from Commons Daemon, with various parameters as indicated.

Because the service silently installed, you have to go to Control Panel, Administrative Tools, Services, and you should see the Tomcat service listed as Tomcat5, as defined in the script.

Running the Tomcat Service

From the Services window, Control Panel, Administrative Tools, Services, you can set Tomcat to start automatically when the machine boots. You can also manually start and stop Tomcat from this window, or if you prefer, you can run the service from the command line and start Tomcat with

net start Tomcat5

You can stop Tomcat with

net stop Tomcat5

Finally, if you want to remove the service, just type

"%CATALINA_HOME%\bin\tomcat //RS/Tomcat5 

Depending on how you installed the service, you might have a different name from mine. When I install it manually, I usually call the Tomcat 5 service Tomcat5, but the Windows installer calls it Apache Tomcat

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020