Co-operating with Vendor initscripts

From Cfwiki

Jump to: navigation, search

(Based on an article written for Anchor Shipyard)

Installing a daemon package under UNIX or Linux does not mean that daemon will start up automatically the next time the system is started. If an automatic startup is desired, there are some more steps needed to make sure that this occurs.

On Red Hat systems, the chkconfig tool enables and disables services. It is possible to ensure not only that a daemon is currently running, but that it will also start automatically on reboot.

For a daemon (for example, //foobar//) the processes section is fairly straight-forward and standard:

 processes:
 
   foobar_server::
 
     "foobard" restart "/sbin/service foobar restart"
       elsedefine=foobar_chkconfig_on

The installable class above gets defined if the //foobard// service wasn't running. Now, in shellcommands, we have a few commands to complement this processes block:

 shellcommands:
 
   foobar_server.foobar_chkconfig_on.redhat::
 
     "/sbin/chkconfig foobar on" useshell=false

So if the service wasn't running, then make sure it will be next time the machine reboots. Additionally, we can query chkconfig directly; this is useful for services that run from inetd (or xinetd) that aren't long running:

 shellcommands:
 
   foobar_server.redhat::
 
     "/sbin/chkconfig --list foobar | grep off" useshell=true
       inform=false
       define=foobar_chkconfig_on

The "off" just matches the output of chkconfig on that service; long running processes can be checked with "3:off" or whatever runlevel one wants the service at... though this is not terribly useful with a processes command (though it may trigger the foobar_chkconfig_on class earlier than processes does). "inform=false" just asks cfengine to ignore the output, because we're not terribly interested in the output of chkconfig every time cfengine runs.

On the other hand, we may want to turn off some services if they're found to be running or configured to start at boot, in which case the tests are reversed:

 processes:
 
   !foobar_server::
 
     "foobard"
       matches=0
       action=bymatch
       define=foobar_chkconfig_off
 
 shellcommands:
 
   !foobar_server.redhat::
 
     "/sbin/chkconfig --list foobar | grep on" useshell=true
       inform=false
       define=foobar_chkconfig_off
 
   !foobar_server.foobar_chkconfig_off.redhat::
 
     "/sbin/chkconfig foobar off" useshell=false

The "matches" and "action" options to the processes clause here are a short hack to avoid requiring a phony "restart" option, as per this post by David Nelson on the cfengine mailing list.


Note that chkconfig can be directly queried for whether a package is on or off, rather than grepping the list output for "on". That is, to make sure a Red Hat service is currently running and will also be started on the next boot, but only the appropriate servers, do:

 shellcommands:
   "/sbin/service foobar status > /dev/null"
     define=foobar_service_is_on
   "/sbin/chkconfig foobar"
     define=foobar_chkconfig_is_on
   foobar_server.!foobar_service_is_on::
     "/sbin/service foobar start"
   foobar_server.!foobar_chkconfig_is_on::
     "/sbin/chkconfig foobar on"
   !foobar_server.foobar_service_is_on::
     "/sbin/service foobar stop"
   !foobar_server.foobar_chkconfig_is_on::
     "/sbin/chkconfig foobar off"

- Matt West


This tip is only good for [Red Hat] systems. Also, while this code snippet may work correctly:

 /sbin/chkconfig foobar

the following will not:

 /sbin/chkconfig foobar | grep "off"

because the output will have all run levels listed, many of which are //off//.

Personal tools