RC.SUBR(8)                 BSD System Manager's Manual                RC.SUBR(8)

     rc.subr — functions used by system shell scripts

     . /etc/rc.subr

     backup_file action file current backup
     checkyesno var
     check_pidfile pidfile procname [interpreter]
     check_process procname [interpreter]
     debug message
     err exitval message
     force_depend name
     info message
     load_kld [-e regex] [-m module] file
     load_rc_config name
     load_rc_config_var name var
     mount_critical_filesystems type
     rc_usage command ...
     reverse_list item ...
     run_rc_command argument
     run_rc_script file argument
     wait_for_pids [pid ...]
     warn message

     The rc.subr script contains commonly used shell script functions and
     variable definitions which are used by various scripts such as rc(8).
     Scripts required by ports in /usr/local/etc/rc.d will also eventually be
     rewritten to make use of it.

     The rc.subr functions were mostly imported from NetBSD.

     They are accessed by sourcing /etc/rc.subr into the current shell.

     The following shell functions are available:

     backup_file action file current backup
           Make a backup copy of file into current.  Save the previous version
           of current as backup.

           The action argument may be one of the following:

           add     file is now being backed up by or possibly re-entered into
                   this backup mechanism.  current is created.

           update  file has changed and needs to be backed up.  If current
                   exists, it is copied to backup and then file is copied to

           remove  file is no longer being tracked by this backup mechanism.
                   current is moved to backup.

     checkyesno var
           Return 0 if var is defined to “YES”, “TRUE”, “ON”, or ‘1’.  Return 1
           if var is defined to “NO”, “FALSE”, “OFF”, or ‘0’.  Otherwise, warn
           that var is not set correctly.  The values are case insensitive.
           Note: var should be a variable name, not its value; checkyesno will
           expand the variable by itself.

     check_pidfile pidfile procname [interpreter]
           Parses the first word of the first line of pidfile for a PID, and
           ensures that the process with that PID is running and its first
           argument matches procname.  Prints the matching PID if successful,
           otherwise nothing.  If interpreter is provided, parse the first line
           of procname, ensure that the line is of the form:

                 #! interpreter [...]

           and use interpreter with its optional arguments and procname appended
           as the process string to search for.

     check_process procname [interpreter]
           Prints the PIDs of any processes that are running with a first
           argument that matches procname.  interpreter is handled as per

     debug message
           Display a debugging message to stderr, log it to the system log using
           logger(1), and return to the caller.  The error message consists of
           the script name (from $0), followed by “: DEBUG: ”, and then message.
           This function is intended to be used by developers as an aid to
           debugging scripts.  It can be turned on or off by the rc.conf(5)
           variable rc_debug.

     err exitval message
           Display an error message to stderr, log it to the system log using
           logger(1), and exit with an exit value of exitval.  The error message
           consists of the script name (from $0), followed by “: ERROR: ”, and
           then message.

     force_depend name
           Output an advisory message and force the name service to start.  The
           name argument is the basename(1) component of the path to the script,
           usually /etc/rc.d/name.  If the script fails for any reason it will
           output a warning and return with a return value of 1.  If it was
           successful it will return 0.

     info message
           Display an informational message to stdout, and log it to the system
           log using logger(1).  The message consists of the script name (from
           $0), followed by “: INFO: ”, and then message.  The display of this
           informational output can be turned on or off by the rc.conf(5)
           variable rc_info.

     load_kld [-e regex] [-m module] file
           Load file as a kernel module unless it is already loaded.  For the
           purpose of checking the module status, either the exact module name
           can be specified using -m, or an egrep(1) regular expression matching
           the module name can be supplied via -e.  By default, the module is
           assumed to have the same name as file, which is not always the case.

     load_rc_config name
           Source in the configuration files for name.  First, /etc/rc.conf is
           sourced if it has not yet been read in.  Then, /etc/rc.conf.d/name is
           sourced if it is an existing file.  The latter may also contain other
           variable assignments to override run_rc_command arguments defined by
           the calling script, to provide an easy mechanism for an administrator
           to override the behaviour of a given rc.d(8) script without requiring
           the editing of that script.

     load_rc_config_var name var
           Read the rc.conf(5) variable var for name and set in the current
           shell, using load_rc_config in a sub-shell to prevent unwanted side
           effects from other variable assignments.

     mount_critical_filesystems type
           Go through a list of critical file systems, as found in the
           rc.conf(5) variable critical_filesystems_type, mounting each one that
           is not currently mounted.

     rc_usage command ...
           Print a usage message for $0, with commands being the list of valid
           arguments prefixed by “[fast|force|one|quiet]”.

     reverse_list item ...
           Print the list of items in reverse order.

     run_rc_command argument
           Run the argument method for the current rc.d(8) script, based on the
           settings of various shell variables.  run_rc_command is extremely
           flexible, and allows fully functional rc.d(8) scripts to be
           implemented in a small amount of shell code.

           argument is searched for in the list of supported commands, which may
           be one of:

                 start    Start the service.  This should check that the service
                          is to be started as specified by rc.conf(5).  Also
                          checks if the service is already running and refuses
                          to start if it is.  This latter check is not performed
                          by standard FreeBSD scripts if the system is starting
                          directly to multi-user mode, to speed up the boot

                 stop     If the service is to be started as specified by
                          rc.conf(5), stop the service.  This should check that
                          the service is running and complain if it is not.

                 restart  Perform a stop then a start.  Defaults to displaying
                          the process ID of the program (if running).

                 enabled  Return 0 if the service is enabled and 1 if it is not.
                          This command does not print anything.

                 rcvar    Display which rc.conf(5) variables are used to control
                          the startup of the service (if any).

           If pidfile or procname is set, also support:

                 poll     Wait for the command to exit.

                 status   Show the status of the process.

           Other supported commands are listed in the optional variable

           argument may have one of the following prefixes which alters its

                 fast   Skip the check for an existing running process, and sets

                 force  Skip the checks for rcvar being set to “YES”, and sets
                        rc_force=YES.  This ignores argument_precmd returning
                        non-zero, and ignores any of the required_* tests
                        failing, and always returns a zero exit status.

                 one    Skip the checks for rcvar being set to “YES”, but
                        performs all the other prerequisite tests.

                 quiet  Inhibits some verbose diagnostics.  Currently, this
                        includes messages "Starting ${name}" (as checked by
                        check_startmsgs inside rc.subr) and errors about usage
                        of services that are not enabled in rc.conf(5).  This
                        prefix also sets rc_quiet=YES.  Please, note: rc_quiet
                        is not intended to completely mask all debug and warning
                        messages, but only certain small classes of them.

           run_rc_command uses the following shell variables to control its
           behaviour.  Unless otherwise stated, these are optional.

                 name      The name of this script.  This is not optional.

                 rcvar     The value of rcvar is checked with checkyesno to
                           determine if this method should be run.

                 command   Full path to the command.  Not required if
                           argument_cmd is defined for each supported keyword.
                           Can be overridden by ${name}_program.

                           Optional arguments and/or shell directives for

                           command is started with:

                                 #! command_interpreter [...]

                           which results in its ps(1) command being:

                                 command_interpreter [...] command

                           so use that string to find the PID(s) of the running
                           command rather than command.

                           Extra commands/keywords/arguments supported.

                 pidfile   Path to PID file.  Used to determine the PID(s) of
                           the running command.  If pidfile is set, use:

                                 check_pidfile $pidfile $procname

                           to find the PID.  Otherwise, if command is set, use:

                                 check_process $procname

                           to find the PID.

                 procname  Process name to check for.  Defaults to the value of

                           Check for the existence of the listed directories
                           before running the start method.

                           Check for the readability of the listed files before
                           running the start method.

                           Ensure that the listed kernel modules are loaded
                           before running the start method.  This is done after
                           invoking the commands from start_precmd so that the
                           missing modules are not loaded in vain if the
                           preliminary commands indicate a error condition.  A
                           word in the list can have an optional “:modname” or
                           “~pattern” suffix.  The modname or pattern parameter
                           is passed to load_kld through a -m or -e option,
                           respectively.  See the description of load_kld in
                           this document for details.

                           Perform checkyesno on each of the list variables
                           before running the start method.

                           Directory to cd to before running command, if
                           ${name}_chroot is not provided.

                           Directory to chroot(8) to before running command.
                           Only supported after /usr is mounted.

                           A list of environment variables to run command with.
                           This will be passed as arguments to the env(1)

                           A file to source for environmental variables to run
                           command with.  Note that all the variables which are
                           being assigned in this file are going to be exported
                           into the environment of command.

                           FIB Routing Table number to run command with.  See
                           setfib(1) for more details.

                           Arguments to call command with.  This is usually set
                           in rc.conf(5), and not in the rc.d(8) script.  The
                           environment variable ‘flags’ can be used to override

                           nice(1) level to run command as.  Only supported
                           after /usr is mounted.

                           Resource limits to apply to command.  This will be
                           passed as arguments to the limits(1) utility.  By
                           default, the resource limits are based on the login
                           class defined in ${name}_login_class.

                           Login class to use with ${name}_limits.  Defaults to

                           protect(1) command from being killed when swap space
                           is exhausted.  If “YES” is used, no child processes
                           are protected.  If “ALL”, protect all child

                           Full path to the command.  Overrides command if both
                           are set, but has no effect if command is unset.  As a
                           rule, command should be set in the script while
                           ${name}_program should be set in rc.conf(5).

                           User to run command as, using chroot(8) if
                           ${name}_chroot is set, otherwise uses su(1).  Only
                           supported after /usr is mounted.

                           Group to run the chrooted command as.

                           Comma separated list of supplementary groups to run
                           the chrooted command with.

                           Commands to be prepended to command.  This is a
                           generic version of ${name}_env, ${name}_fib, or

                           Shell commands which override the default method for

                           Shell commands to run just before running
                           argument_cmd or the default method for argument.  If
                           this returns a non-zero exit code, the main method is
                           not performed.  If the default method is being
                           executed, this check is performed after the
                           required_* checks and process (non-)existence checks.

                           Shell commands to run if running argument_cmd or the
                           default method for argument returned a zero exit

                 sig_stop  Signal to send the processes to stop in the default
                           stop method.  Defaults to SIGTERM.

                           Signal to send the processes to reload in the default
                           reload method.  Defaults to SIGHUP.

           For a given method argument, if argument_cmd is not defined, then a
           default method is provided by run_rc_command:

                 Argument  Default method

                 start     If command is not running and checkyesno rcvar
                           succeeds, start command.

                 stop      Determine the PIDs of command with check_pidfile or
                           check_process (as appropriate), kill sig_stop those
                           PIDs, and run wait_for_pids on those PIDs.

                 reload    Similar to stop, except that it uses sig_reload
                           instead, and does not run wait_for_pids.  Another
                           difference from stop is that reload is not provided
                           by default.  It can be enabled via extra_commands if


                 restart   Runs the stop method, then the start method.

                 status    Show the PID of command, or some other script
                           specific status operation.

                 poll      Wait for command to exit.

                 rcvar     Display which rc.conf(5) variable is used (if any).
                           This method always works, even if the appropriate
                           rc.conf(5) variable is set to “NO”.

           The following variables are available to the methods (such as
           argument_cmd) as well as after run_rc_command has completed:

                 rc_arg      Argument provided to run_rc_command, after fast and
                             force processing has been performed.

                 rc_flags    Flags to start the default command with.  Defaults
                             to ${name}_flags, unless overridden by the
                             environment variable ‘flags’.  This variable may be
                             changed by the argument_precmd method.

                 rc_service  Path to the service script being executed, in case
                             it needs to re-invoke itself.

                 rc_pid      PID of command (if appropriate).

                 rc_fast     Not empty if “fast” prefix was used.

                 rc_force    Not empty if “force” prefix was used.

     run_rc_script file argument
           Start the script file with an argument of argument, and handle the
           return value from the script.

           Various shell variables are unset before file is started:

                 name, command, command_args, command_interpreter,
                 extra_commands, pidfile, rcvar, required_dirs, required_files,
                 required_vars, argument_cmd, argument_precmd.

           The startup behaviour of file depends upon the following checks:

           1.   If file ends in .sh, it is sourced into the current shell.

           2.   If file appears to be a backup or scratch file (e.g., with a
                suffix of ~, #, .OLD, or .orig), ignore it.

           3.   If file is not executable, ignore it.

           4.   If the rc.conf(5) variable rc_fast_and_loose is empty, source
                file in a sub shell, otherwise source file into the current

     stop_boot [always]
           Prevent booting to multiuser mode.  If the autoboot variable is set
           to ‘yes’, or checkyesno always indicates a truth value, then a
           SIGTERM signal is sent to the parent process, which is assumed to be
           rc(8).  Otherwise, the shell exits with a non-zero status.

     wait_for_pids [pid ...]
           Wait until all of the provided pids do not exist any more, printing
           the list of outstanding pids every two seconds.

     warn message
           Display a warning message to stderr and log it to the system log
           using logger(1).  The warning message consists of the script name
           (from $0), followed by “: WARNING: ”, and then message.

     /etc/rc.subr  The rc.subr file resides in /etc.

     rc.conf(5), rc(8)

     The rc.subr script appeared in NetBSD 1.3.  The rc.d(8) support functions
     appeared in NetBSD 1.5.  The rc.subr script first appeared in FreeBSD 5.0.

BSD                             January 15, 2019                             BSD