Solaris tip of the week: SMF
These 'tips of the week' are intended to provide shortcuts to simplify your use of some basic (and not so basic) Solaris features.
I'll start with something simple - the service management facility, SMF. SMF is responsible for starting and monitoring an application deployed on Solaris, and restarting the application if it terminates. The start/stop instructions for your application are defined in an XML manifest, which is imported with the
'svccfg' command. I've highlighted in red the definitions that you will change when modifying the template for your own application.
Example: Launch a glassfish server when the system boots, but only after the mysql service is running. If the glassfish server terminates, restart it.
The smf manifest template, a file named 'glassfish.xml':
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<!--
ident "@(#)template.xml 1.0 08/06/05 SMI"
-->
<service_bundle type='manifest' name='glassfish'>
<service
name='application/uc/glassfish'
type='service'
version='1'>
<create_default_instance enabled='true'/>
<single_instance/>
<dependency name='mysql'
grouping='require_all'
restart_on='error'
type='service'>
<service_fmri value='svc:/application/database/mysql:version_50'/>
</dependency>
<exec_method type='method' name='start'
exec='/opt/glassfish/bin/asadmin start-domain domain1'
timeout_seconds='60' />
<exec_method type='method' name='stop'
exec='/opt/glassfish/bin/asadmin stop-domain domain1'
timeout_seconds='60' />
<property_group name='startd' type='framework'>
<!-- sub-process core dumps shouldn't restart session -->
<propval name='ignore_error' type='astring' value='core,signal' />
</property_group>
<stability value='Evolving' />
<template>
<common_name>
<loctext xml:lang='C'>
glassfish
</loctext>
</common_name>
</template>
</service>
</service_bundle>
This manifest should fit most of your needs. You can modify the highlighted attributes to adapt this manifest for your own application.
Note on dependencies: The mysql dependency refers to the mysql service bundled with recent builds of OpenSolaris nevada. When an application does not have a dependency, I use dependency_name='multi-user-server' and service_fmri='svc:/milestone/multi-user-server:default'.
You're ready to go - import the manifest to start the application:
# svccfg import glassfish.xml
# svcs glassfish
STATE STIME FMRI
online 9:08:27 svc:/application/uc/glassfish:default
I'm still looking for the value-add of SMF. So far, it allows me to do less than RC scripts (limited actions available) with the added benefit that I must write XML in addition to sh. With initd, I only had to know sh. On top of that, I can take it to no other type of system, whereas sh I can use anywhere.
-Ken
Posted by Ken Marsh on August 25, 2008 at 04:54 PM EDT #
Hi Ken,
I don't view SMF as an either/or proposition. SMF extends your existing scripts - no need to rewrite or port them - continue to use them as they are. This post was intended to provide the boilerplate XML to wrap your existing script, so there's no 'effort' required. I can think of 3 compelling value-add reasons associated with SMF off the top of my head:
- Service monitoring: If my service dies, it is automatically restarted by SMF.
- SMF identifies processes associated with a service: Have you ever looked at the output of 'ps -ef' and had a long listing of 'java' procs or 'httpd' procs and had a hard time determining which service each process corresponded to - for example, if you run multiple app servers and want to restart or kill one particular server ? It's not always easy to distinguish which java proc corresponds to which service. SMF makes it incredibly easy to identify the processes associated with a given service: svcs -p [myservice]
- dependency management: IMO, it's easier to identify dependencies associated with my service - in this example I list mysql as a dependency. If my service fails to start, is it a problem with my service or because a dependent service failed to start ? 'svcs -d [myservice]' provides the answer to this question.
HTH
Posted by Jay Danielsen on September 05, 2008 at 11:25 AM EDT #