pavement

Usr/local/etc/rc.d

From FreeBSDwiki
Revision as of 17:43, 9 May 2006 by Ninereasons (Talk | contribs)
Jump to: navigation, search

/usr/local/etc/rc.d isn't actually a config file, it's a directory. Any executable shell script in this directory will be executed (using the Bourne shell) with the argument "start" when the system boots, and again with the argument "stop" when the system is shut down.

You may find this generic rc.d script (shown as I use it to start bind9) handy for general purpose use:

#!/bin/sh

case "$1" in
start)
        /usr/sbin/named -c /etc/namedb/named.conf &
        echo "bind9"
        exit 0
        ;;
stop)
        exec killall named
        ;;
*)
        echo "Usage: `basename $0` {start|stop}" >&2
        exit 64
        ;;
esac

Note that scripts in /usr/local/etc/rc.d cannot and will not execute, at boot time or otherwise, if you do not set the permissions to allow their execution. In most cases, you will want to chmod 755 any rc.d scripts, though you may also consider 700 on root-owned rc.d scripts to make sure that unprivileged users don't mess with attempting to start or stop sensitive services.

rc.subr

rc.subr is a subroutine library that was developed primarily for the NetBSD system and imported into FreeBSD where it was further developed. See man rc.subr

Under FreeBSD 4.x, you may have noticed this file being installed into /usr/local/etc/rc.subr, where it was increasingly used by port-installed daemons. Beginning in FreeBSD 5.0, this file became integral to the FreeBSD startup mechanism for both, base-installed services and port-installed services. At the same time, the directory /etc/rc.d appeared in FreeBSD, where you will find startup scripts for all base-installed services (so that, for example, /etc/rc.sendmail disappeared, and was replaced by /etc/rc.d/sendmail, which uses the rc.subr mechanism)

Besides being a repository of some very useful and well-written functions, perhaps the chief advantage of using the file is that it ties the configuration, startup and shutdown of these daemons to /etc/rc.conf in a consistent way. For example, unless the service has an ="YES" line in rc.conf, the start and stop commands have no effect.

Some folks don't like starting all of their services from /etc/rc.conf. rc.subr accommodates this preference with the prefixes, force and one.

Another significant change that this mechanism introduces is, if a startup script ends with .sh, it will be loaded into the current shell (instead of a subshell); N.B. : this means that if script.sh fails, the remainder of the startup sequence will not be executed.

Finally, a valuable improvement to service startup is better control over the order in which daemons are started. See man rc(8) and man rcorder(8)

Examples

1. Minimal rc.d/ script example, using the new rc.subr mechanism. Most scripts require little more than this.

  #!/bin/sh
  #
  # PROVIDE: foo
  # REQUIRE: bar_service_required_to_precede_foo
  # BEFORE:  baz_service_requiring_foo_to_precede_it
  
  . /etc/rc.subr
  
  name="foo"
  rcvar=`set_rcvar`
  command="/usr/local/bin/foo"
  load_rc_config $name
  run_rc_command "$1"

2. Manually start and stop mysql, even though mysql_enable is set to "NO" (default) in rc.conf.

  • Skip all failing pre-requisite "required_*" tests (if any).
# /usr/local/etc/rc.d/mysql-server forcestart
# /usr/local/etc/rc.d/mysql-server forcestop
  • Skip only the ="YES" test
# /usr/local/etc/rc.d/mysql-server onestart
# /usr/local/etc/rc.d/mysql-server onestop
Personal tools