We said earlier that rc.subr(8) could provide default methods. Obviously, such defaults cannot be too general. They are suited for the common case of starting and shutting down a simple daemon program. Let us assume now that we need to write an rc.d script for such a daemon called mumbled. Here it is:
#!/bin/sh . /etc/rc.subr name=mumbled rcvar=mumbled_enable command="/usr/sbin/${name}" load_rc_config $name run_rc_command "$1"
Pleasingly simple, isn't it? Let us examine our little script. The only new thing to note is as follows:
start
, stop
, restart
, poll
, and status
.The daemon will be started by running $command with
command-line flags specified by $mumbled_flags. Thus all the
input data for the default start
method are
available in the variables set by our script. Unlike start
, other methods may require additional information about
the process started. For instance, stop
must know the
PID of the process to terminate it. In the present case, rc.subr(8) will
scan through the list of all processes, looking for a process with its name equal
to $procname. The latter is another variable of meaning to
rc.subr(8), and
its value defaults to that of command. In other words, when
we set command, procname is
effectively set to the same value. This enables our script to kill the daemon and
to check if it is running in the first place.
Note: Some programs are in fact executable scripts. The system runs such a script by starting its interpreter and passing the name of the script to it as a command-line argument. This is reflected in the list of processes, which can confuse rc.subr(8). You should additionally set command_interpreter to let rc.subr(8) know the actual name of the process if $command is a script.
For each rc.d script, there is an optional rc.conf(5) variable that takes precedence over command. Its name is constructed as follows: ${name}_program, where name is the mandatory variable we discussed earlier. E.g., in this case it will be mumbled_program. It is rc.subr(8) that arranges ${name}_program to override command.
Of course, sh(1) will permit you to set ${name}_program from rc.conf(5) or the script itself even if command is unset. In that case, the special properties of ${name}_program are lost, and it becomes an ordinary variable your script can use for its own purposes. However, the sole use of ${name}_program is discouraged because using it together with command became an idiom of rc.d scripting.
For more detailed information on default methods, refer to rc.subr(8).