rc.d

From NetBSD Wiki

Jump to: navigation, search

rc.d(8) is both the collective term for NetBSD's system of managing system services' startup/shutdown and the name of the directory which contains these scripts. The current system was introduced in NetBSD 1.5.

Contents

Startup

The rc.d system is elegant and simple to understand: When the system boots up, this happens:

  • After the kernel boots and performs autoconf and other initialization, init is forked off.
  • init starts the /etc/rc script.
  • rc invokes rcorder to determine in which order to execute the scripts from /etc/rc.d.
  • rc invokes all of these scripts in an order consistent with the specifications, with the commandline argument of "start".
  • The scripts check rc.conf if they're supposed to run.
  • If so, the daemon's binary (or the required shellscripts/commands) is invoked with the arguments from rc.conf, if any.

Custom startup code which is not suitable to put in rc.d can be put in /etc/rc.local, which is initially empty. Note that rc.local is simply called from yet another script in rc.d: rc.local

Shutdown

When the system is to be cleanly shut down, certain services must be stopped. rc.d was designed to handle startup as well as shutdown, so this is taken care of too. If you run the shutdown command it will invoke /etc/rc.shutdown, which does the more or less the same thing as /etc/rc, only in reversed order (because you can't just pull out services from under the feet of services who depend on those). Local custom code to be performed upon shutdown is supposed to go in /etc/rc.shutdown.local.

Framework

As you can see from the files in /etc/rc.d, scripts which simply need to invoke a binary don't explicitly call it from that script. They just set a couple of variables and then call load_rc_config followed by run_rc_command. These are functions defined in /etc/rc.subr, which all of these scripts source. This is a framework which takes care of the common tasks of checking the rc.conf variable, looking at the arguments supplied to the scripts to see what should be done (kill, start, etc), kill the daemon based on a PID-file and start the daemon.

Here's a short list of the most important variables defined by rc.subr:

name 
The name of the service. Also used as a basename for _flags, _group, _nice and other suffixes to use in rc.conf.
rcvar 
The name of the variable in rc.conf that controls startup of this service. Can be printed by invoking /etc/rc.d/service_name rcvar.
command 
The location of the daemon's binary.
command_args 
Any arguments that must always be supplied when running the command.

For a complete listing, see the rc.subr(8)} manpage or its source code.

Manually starting or stopping services

Manually starting or stopping a service is easy. You can just run

# /etc/rc.d/service_name start

or

# /etc/rc.d/service_name stop

to start or stop a service, but only if you have it enabled in /etc/rc.conf. Otherwise it will simply do nothing.

If you wish to start a service just once, use

# /etc/rc.d/service_name onestart

and to stop it,

# /etc/rc.d/service_name onestop

If you must override dependency or other sanity checking, you can force it:

# /etc/rc.d/service_name forcestart
# /etc/rc.d/service_name forcestop

To check if a service is running or not, you can use status:

# /etc/rc.d/service_name status

For a more complete list of applicable commands and command prefixes, see the rc.d(8) manual page and invoke the particular service script without any arguments. This will also show you the commands that are unique to that specific service.


See also

Retrieved from "http://wiki.netbsd.se/rc.d"
Personal tools