For my Tomcat SMF setup, I have two files.  One, the configuration for SMF.  The second is the script used to start and restart tomcat. (My stop is simply a kill. Unfortunately, there are a few threads that aren’t shutting down properly.)

First, the configuration:

<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM
'/usr/share/lib/xml/dtd/service_bundle.dtd.1'>

<service_bundle type='manifest' name='Tomcat'>

<service
	name='network/tomcat'
	type='service'
	version='1'>

	<create_default_instance enabled='false' />
	<single_instance />

	<dependency name='fs'
		grouping='require_all'
		restart_on='none'
		type='service'>
		<service_fmri value='svc:/system/filesystem/local' />
	</dependency>

	<dependency name='net'
		grouping='require_all'
		restart_on='none'
		type='service'>
		<service_fmri value='svc:/network/loopback' />
	</dependency>

	<exec_method
		type='method'
		name='start'
		exec='/opt/local/lib/svc/method/svc-tomcat start'
		timeout_seconds='-1'>
		<method_context>
			<method_credential user='root' group='root' />
		</method_context>
	</exec_method>

	<exec_method
		type='method'
		name='stop'
		exec=':kill'
		timeout_seconds='-1'>
	</exec_method>

	<exec_method
		type='method'
		name='restart'
		exec='/opt/local/lib/svc/method/svc-tomcat restart'
		timeout_seconds='-1'>
	</exec_method>

	<stability value='Unstable' />

	<template>
		<common_name>
			<loctext xml:lang='C'>
				tomcat
			</loctext>
		</common_name>
	</template>
</service>
</service_bundle>

The shell script looks like:

#!/usr/bin/sh

. /lib/svc/share/smf_include.sh

case $1 in
'start')
	echo "starting tomcat..."
	/opt/local/share/tomcat/bin/startup.sh & >/dev/null 2> /dev/null
	;;

'stop')
	echo "stopping tomcat..."
	/opt/local/share/tomcat/bin/shutdown.sh &
;;

'restart')
	stop
# give stuff some time to stop before we restart
	sleep 25
	start
	;;
*)
	echo "Usage: $0 { start | stop | restart }"
	exit 1
	;;
esac

Put tomcat.xml in /var/svc/manifest/network/tomcat.xml
Put svc-tomcat in /opt/local/lib/svc/method/svc-tomcat

I had to create the local svc/method directory so before I could put svc-tomcat where it needed to be.

Then run the following (likely with sudo):

chown root:bin /opt/local/lib/svc/method/svc-tomcat
chmod 555 /opt/local/lib/svc/method/svc-tomcat
chown root:sys /var/svc/manifest/network/tomcat.xml 
chmod 444 /var/svc/manifest/network/tomcat.xml
svccfg import /var/svc/manifest/network/tomcat.xml 

That’s it. You can check on the service with:

[user@host ~]$ svcs -xv tomcat

And start it via:

[user@host ~]$ svcadm enable tomcat
About the Author:

Learned something? Great! Need help on your development project? I'm available for hire:

  • Ruby on Rails
  • iOS Development
  • System Architecture & Performance

Get in touch:

Discussion

  1. Antonio Gonzalez says:

    Great example! Besides having tomcat managed using SMF, i will also use your example to manage informix as well

  2. Andrei Ghimus says:

    You can try “bin/shutdown.sh -force” to kill problematic threads.

Leave a Comment