Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Running Tomcat on Macintosh OS X

Wiki Markup
\[See bottom halfbelow for alater 2006 and 2008 update updates to this 2004 posting\]

These notes are the result of several weeks playing with different things and asking a LOT of questions on several mailing lists.

...

The following assumes you have Java already installed - it should be installed by default on every MacOS X installation. Note that MacOS X 10.2 (Jaguar) comes with Java 1.3; MacOS X 10.3 (Panther) comes with Java 1.4. MacOS X 10.4 (Tiger) comes with Java 1.4.2 installed, but Java 1.5 can be downloaded. MacOS X 10.5 (Leopard) comes with Java 1.5. It is possible to run Java 1.4 on MacOS X Jaguar but it may interfere with operation of the standard environment.

Verify the following paragraph! This explanation, however, should be right, or nearly so.
JAVA_HOME is at /Library/Java/Home, but this is a link into a directory /System/Frameworks/JavaJVM.Framework/Versions which is used to switch between versions easily. There is a link Current and CurrentVersion which matches up the current Java environment with the appropriate version.

...

No Format
<?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>Label</key>
	<string>com.apache.tomcat</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Library/Tomcat/bin/startup.sh</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

Updated for 2009

Greg Woolsey says:

For Tomcat 5.5.x (and probably 6.0, as the scripts don't appear to have changed much) you need a slightly modified plist, that calls catalina.sh in a manner compatible with the requirements of launchd: http://developer.apple.com/MacOsX/launchd.html. Specifically, the script must be run with the "run" parameter instead of "start", as start backgrounds the Java process and exits the script. This causes launchd to think the service is done, and kills the java process as part of it's cleanup.

My plist assumes Tomcat is installed in /Library/Tomcat/tomcat-version, with a symbolic link called "Home" pointing to the version to use.

Save this file as /Library/LaunchDaemons/org.apache.tomcat.plist:

No Format

<?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>Label</key>
	<string>org.apache.tomcat</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Library/Tomcat/Home/bin/catalina.sh</string>
		<string>run</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

If you have a web app that doesn't want to shut down nicely (my company's product is one), then to avoid the need to force quit Tomcat just to turn off your Mac, use a startup script instead of a daemon plist.

Add this line to /etc/hostconfig as root:

TOMCAT=YES

Then, as root, create /Library/StartupItems/Tomcat, writable only by the owner (root).

Inside this, create two files - Tomcat (script) and StartupParameters.plist. Again, make them writable only by root, and the script executable by everyone.

Contents of StartupParameters.plist:

No Format

<?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>Description</key>
	<string>Tomcat Server</string>
	<key>OrderPreference</key>
	<string>Late</string>
	<key>Provides</key>
	<array>
		<string>Tomcat</string>
	</array>
	</dict>
</plist>

Contents of the Tomcat script file:

No Format

#!/bin/sh
#
# /Library/StartupItems/Tomcat/Tomcat
#
# A script to automatically start up Tomcat on system bootup
# for Mac OS X. This is actually just a wrapper script around
# the standard catalina.sh script, which is included in
# the distribution.
#

# Suppress the annoying "$1: unbound variable" error when no option
# was given
if [ -z $1 ] ; then
	echo "Usage: $0 [start|stop|restart] "
	exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common

# The path to the catalina.sh script. 
# The currently used version is in /Library/Tomcat/Home/bin
SCRIPT="/Library/Tomcat/Home/bin/catalina.sh"

# file to hold the process ID on start so it can be killed by stop.
export CATALINA_PID="/Library/Tomcat/Home/server.pid"

StartService ()
{
	if [ "${TOMCAT:=-NO-}" = "-YES-" ] ; then
		ConsoleMessage "Starting Tomcat server"
		$SCRIPT start > /dev/null 2>&1
	fi
}

StopService ()
{
	ConsoleMessage "Stopping Tomcat server"
	$SCRIPT stop -force > /dev/null 2>&1
}

RestartService ()
{
	ConsoleMessage "Restarting Tomcat server"
	StopService
	StartService
}

if test -x $SCRIPT ; then
	RunService "$1"
else
	ConsoleMessage "Could not find Tomcat control script!"
fi