pavement

Usr/local/etc/rc.d

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Examples)
(Moving section to a separate article)
Line 22: Line 22:
 
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.
 
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==
+
The most recent versions of FreeBSD make [[rc.conf]] more integral to the startup of local services. For information about the way that [[daemon]]s are started in the newest versions of FreeBSD, see [[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 [http://www.freebsd.org/cgi/man.cgi?query=rc.subr&apropos=0&sektion=0&manpath=FreeBSD+6.0-RELEASE+and+Ports&format=html 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 <code>="YES"</code> line in ''rc.conf'', the ''start'' and ''stop'' commands have no effect. 
+
 
+
[[User:Jimbo|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 [[daemon]]s are started.  See [http://www.freebsd.org/cgi/man.cgi?query=rc&sektion=8&apropos=0&manpath=FreeBSD+6.0-RELEASE+and+Ports man rc(8)] and [http://www.freebsd.org/cgi/man.cgi?query=rcorder&sektion=8&apropos=0&manpath=FreeBSD+6.0-stable 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 <code>mysql_enable</code> is set to <code>"NO"</code> (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 <code>="YES"</code> 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/*
+
  
 
[[Category:Important Config Files]]
 
[[Category:Important Config Files]]

Revision as of 16:40, 20 June 2006

/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.

The most recent versions of FreeBSD make rc.conf more integral to the startup of local services. For information about the way that daemons are started in the newest versions of FreeBSD, see rc.subr.

Personal tools