pavement

Rc.subr

From FreeBSDwiki
Jump to: navigation, search

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

3. List the sequence in which all services will be started, reporting missing dependencies:

# rcorder /etc/rc.d/* /usr/local/etc/rc.d/*
Personal tools