Solaris/SMF

From Segfault
Jump to navigation Jump to search

Let's convert the following simple init script to an SMF[1] (Service Management Facility) service:

$ cat /var/tmp/listen.sh 
#!/bin/sh
#
# Listen on tcp4:8080
#
case $1 in start)
        nc -4l 8080 &
        ;;

        stop)
        pkill -f "nc -4l 8080"
        ;;

        status)
        netstat -an -f inet | grep \\.8080
        ;;
esac

The SMF version would be:

$ cat /var/tmp/listen.xml
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>

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

<service
        name='application/listen'
        type='service'
        version='1'>

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

        <instance name='default' enabled='true'>
        <exec_method
                type='method'
                name='start'
                exec='/var/tmp/listen.sh start'
                timeout_seconds='1'>
        </exec_method>

        <exec_method
                type='method'
                name='stop'
                exec=':kill -15'
                timeout_seconds='3'>
        </exec_method>
        </instance>
</service>
</service_bundle>

Validate it:

$ svccfg validate /var/tmp/listen.xml; echo $?
0

With that in place, we can import the .xml file into our SMF framework:

$ svccfg import /var/tmp/listen.xml 
$ svccfg -s application/listen listprop
manifestfiles                     framework
manifestfiles/var_tmp_listen_xml  astring  /var/tmp/listen.xml
general                           framework
general/single_instance           boolean  true
net-physical                      dependency
net-physical/entities             fmri     svc:/network/physical
net-physical/grouping             astring  require_all
net-physical/restart_on           astring  none
net-physical/type                 astring  service

Enabling the service will start it too:

$ svcadm enable application/listen
$ svcs -xv application/listen
svc:/application/listen:default (?)
 State: online since July  2, 2011 12:16:31 AM PDT
   See: /var/svc/log/application-listen:default.log
Impact: None.

Links

References