rc.d scripts are used to start services on system startup, and to give administrators a standard way of stopping, starting and restarting the service. Ports integrate into the system rc.d framework. Details on its usage can be found in the rc.d Handbook chapter. Detailed explanation of available commands is provided in rc(8) and rc.subr(8). Finally, there is an article on practical aspects of rc.d scripting.
One or more rc scripts can be installed:
USE_RC_SUBR= doormand
Scripts must be placed in the files subdirectory and a .in suffix must be added to their filename. The only difference from a base system rc.d script is that the . /etc/rc.subr line must be replaced with the . %%RC_SUBR%%, because older versions of FreeBSD do not have an /etc/rc.subr file. Standard SUB_LIST expansions are used too. Use of the %%PREFIX%% and %%LOCALBASE%% expansions is strongly encouraged as well. More on SUB_LIST in the relevant section.
Prior to FreeBSD 6.1-RELEASE, integration with rcorder(8) is available by using USE_RCORDER instead of USE_RC_SUBR. However, use of this method is deprecated.
As of FreeBSD 6.1-RELEASE, local rc.d scripts (including those installed by ports) are included in the overall rcorder(8) of the base system.
Example simple rc.d script:
#!/bin/sh # PROVIDE: doormand # REQUIRE: LOGIN # # Add the following lines to /etc/rc.conf.local or /etc/rc.conf # to enable this service: # # doormand_enable (bool): Set to NO by default. # Set it to YES to enable doormand. # doormand_config (path): Set to %%PREFIX%%/etc/doormand/doormand.cf # by default. # . %%RC_SUBR%% name="doormand" rcvar=${name}_enable command=%%PREFIX%%/sbin/${name} pidfile=/var/run/${name}.pid load_rc_config $name : ${doormand_enable="NO"} : ${doormand_config="%%PREFIX%%/etc/doormand/doormand.cf"} command_args="-p $pidfile -f $doormand_config" run_rc_command "$1"
The "=" style of default variable assignment is preferable to the ":=" style here, since the former sets a default value only if the variable is unset, and the latter sets one if the variable is unset or null. A user might very well include something like
doormand_flags=""in their rc.conf.local file, and a variable substitution using ":=" would inappropriately override the user's intention.
The suffix of the rc script is provided in RC_SUBR_SUFFIX for further use in the port's Makefile. Current versions of FreeBSD do not add any suffix to the script name, but older versions used to add .sh suffix.
注: No new scripts should be added with the .sh suffix. At some point there will be a mass repocopy of all the scripts that still have that suffix.
It is possible to have a service stopped automatically as part of the deinstall routine. We advise using this feature only when it's absolutely necessary to stop a service before it's files go away. Usually, it's up to the administrator's discretion to decide, whether to stop the service on deinstall or not. Also note this affects upgrades, too.
Line like this goes to the pkg-plist:
@stopdaemon doormand
The argument must match the content of USE_RC_SUBR variable.