RC(8)
π RC(8) β command scripts for auto-reboot and daemon startup π
okay so bestie, welcome to RC(8) from the System Manager's Manual. This is highkey the whole βwhat runs when your box boots or shuts downβ situation, and honestly it's giving main-character energy. Let's get into it. π«Ά
π NAME π
rcβ command scripts for auto-reboot and daemon startup
π¦ SYNOPSIS π¦
rc
rc.conf
rc.conf.local
rc.d/
rc.firewall
rc.local
rc.resume
rc.shutdown
rc.subr
rc.suspend
πΈ DESCRIPTION πΈ
The rc utility is the command script that controls the automatic boot process after init(8) calls it. The rc.local script holds commands that are only about a specific site. These days the /usr/local/etc/rc.d/ mechanism is usually used instead of rc.local, but if you want to use rc.local, it is still supported. In that case it should source /etc/rc.conf and contain extra custom startup code for your system. The best way to handle rc.local, though, is to split it out into rc.d/ style scripts and put them under /usr/local/etc/rc.d/. The rc.conf file has the global system configuration info that the startup scripts reference, while rc.conf.local holds the local system configuration. See rc.conf(5) for more info.
The rc.d/ directories contain scripts that get automatically executed at boot time and shutdown time. β¨
The service(8) command gives you a convenient interface to manage rc.d services. The sysrc(8) command gives you a scripting interface to modify system config files.
βοΈ Operation of rc βοΈ
Here's the boot-time sequence, step by step β no cap:
- If autobooting, set
autoboot=yesand enable a flag (rc_fast=yes), which prevents therc.d/scripts from doing the check for already running processes (so the boot process speeds up). Thisrc_fast=yesspeedup will not happen whenrcis started up after exiting the single-user shell. - Figure out whether the system is booting diskless, and if it is, run the
/etc/rc.initdisklessscript. source /etc/rc.subrto load the variousrc.subr(8)shell functions to use.- Load the configuration files (see below for reloading).
- Determine if booting in a jail, and add βnojailβ (no jails allowed) or βnojailvnetβ (only allow vnet-enabled jails) to the list of
KEYWORDSto skip inrcorder(8). - If the file
${firstboot_sentinel}does not exist, add βfirstbootβ to the list ofKEYWORDSto skip inrcorder(8). - Invoke
rcorder(8)to order the files in/etc/rc.d/that do not have a βnostartβKEYWORD(refer torcorder(8)'s-sflag). - Call
run_rc_scripts()with the list of scripts to be run. For each script that will callrun_rc_script()(both functions are fromrc.subr(8)), which sets$1to βstartβ, and sources the script in a subshell. Stop processing when the script that is the value of the$early_late_dividerhas been run. - Check again to see if the file
${firstboot_sentinel}exists (in case it is located on a newly mounted file system) and adjust the list ofKEYWORDs to skip appropriately. - Re-run
rcorder(8), this time including the scripts in the$local_startupdirectories. Callrun_rc_scripts()again with the new list of scripts. It will skip any that have already been run and execute the rest as described above. - If the file
${firstboot_sentinel}exists, delete it. If the file${firstboot_sentinel}-rebootalso exists (because it was created by a script), then delete it and reboot.
π Operation of rc.shutdown π
Shutdown sequence, bestie β also step by step, and yes it ate:
- Set
rc_shutdownto the value of the first argument passed torc.shutdownor to βunspecifiedβ if no argument was passed. source /etc/rc.subrto load the variousrc.subr(8)shell functions to use.- Load the configuration files.
- Invoke
rcorder(8)to order the files in/etc/rc.d/and the$local_startupdirectories that have a βshutdownβKEYWORD(refer torcorder(8)'s-kflag), reverse that order, and assign the result to a variable. - Call each script in turn using
run_rc_script()(fromrc.subr(8)), which sets$1to βfaststopβ, and sources the script in a subshell.
π Contents of rc.d/ π
rc.d/ is located in /etc/rc.d/. The following file naming conventions are currently used in rc.d/:
ALLUPPERCASEβ Scripts that are βplaceholdersβ to make sure certain operations happen before others. In order of startup, these are:FILESYSTEMSβ Ensure that root and other critical file systems are mounted. This is the default$early_late_divider.NETWORKINGβ Ensure basic network services are running, including general network configuration.SERVERSβ Ensure basic services exist for services that start early (such asnisdomain), because they are required byDAEMONbelow.DAEMONβ Check-point before all general purpose daemons such aslpdandntpd.LOGINβ Check-point before user login services (inetdandsshd), as well as services which might run commands as users (cronandsendmail).
barβ Scripts that are sourced in a subshell. The boot does not stop if such a script terminates with a non-zero status, but a script can stop the boot if necessary by invoking thestop_boot()function (fromrc.subr(8)).
Each script should contain rcorder(8) keywords, especially an appropriate βPROVIDEβ entry, and if necessary βREQUIREβ and βBEFOREβ keywords.
Each script is expected to support at least the following arguments, which are automatically supported if it uses the run_rc_command() function:
startβ Start the service. This should check that the service is to be started as specified byrc.conf(5). It 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 process. Ifforcestartis given, ignore therc.conf(5)check and start anyway.stopβ If the service is to be started as specified byrc.conf(5), stop the service. This should check that the service is running and complain if it is not. Ifforcestopis given, ignore therc.conf(5)check and attempt to stop.restartβ Perform a stop then a start.statusβ If the script starts a process (rather than performing a one-off operation), show the status of the process. Otherwise it is not necessary to support this argument. Defaults to displaying the process ID of the program (if running).enableβ Enable the service inrc.conf(5).disableβ Disable the service inrc.conf(5).deleteβ Remove the service fromrc.conf(5). Ifservice_delete_emptyis set to βYESβ,/etc/rc.conf.d/$servicenamewill be deleted if empty after modification.describeβ Print a short description of what the script does.extracommandsβ Print the script's non-standard commands.pollβ If the script starts a process (rather than performing a one-off operation), wait for the command to exit. Otherwise it is not necessary to support this argument.enabledβ Return0if the service is enabled and1if it is not. This command does not print anything.rcvarβ Display whichrc.conf(5)variables are used to control the startup of the service (if any).
If a script must implement additional commands it can list them in the extra_commands variable, and define their actions in a variable constructed from the command name (see the βEXAMPLESβ section).
The configuration files are normally read only once at the start of a boot sequence; if a script needs to enable or disable any other script that would run later in the sequence, it must send a SIGALRM to the rc process (identified by $RC_PID) to have it re-read the files.
The following key points apply to old-style scripts in /usr/local/etc/rc.d/ β iykyk:
- Scripts are only executed if their
basename(1)matches the shell globbing pattern*.sh, and they are executable. Any other files or directories present within the directory are silently ignored. - When a script is executed at boot time, it is passed the string βstartβ as its first and only argument. At shutdown time, it is passed the string βstopβ as its first and only argument. All
rc.d/scripts are expected to handle these arguments appropriately. If no action needs to be taken at a given time (either boot time or shutdown time), the script should exit successfully and without producing an error message. - The scripts within each directory are executed in lexicographical order. If a specific order is required, numbers may be used as a prefix to the existing filenames, so for example
100.foowould be executed before200.bar; without the numeric prefixes the opposite would be true. - The output from each script is traditionally a space character, followed by the name of the software package being started or shut down, without a trailing newline character.
πΈ SCRIPTS OF INTEREST πΈ
Okay so when an automatic reboot is in progress, rc is invoked with the argument autoboot. One of the scripts run from /etc/rc.d/ is /etc/rc.d/fsck. This script runs fsck(8) with option -p and -F to βpreenβ all the disks of minor inconsistencies resulting from the last system shutdown. If this fails, then checks/repairs of serious inconsistencies caused by hardware or software failure will be performed in the background at the end of the booting process. If autoboot is not set β when going from single-user to multi-user mode for example β the script does not do anything. π
The /etc/rc.d/serial script is used to set any special configurations for serial devices. π
The rc.firewall script is used to configure rules for the kernel based firewall service. It has several possible options:
openβ will allow anyone inclientβ will try to protect just this machinesimpleβ will try to protect a whole networkclosedβ totally disables IP services except vialo0interfaceUNKNOWNβ disables the loading of firewall rulesfilenameβ will load the rules in the given filename (full path required).
Most daemons, including network related daemons, have their own script in /etc/rc.d/, which can be used to start, stop, and check the status of the service. β¨
Any architecture specific scripts, such as /etc/rc.d/apm for example, specifically check that they are on that architecture before starting the daemon. π¦
Following tradition, all startup files reside in /etc. π«Ά
π FILES π
/etc/rc/etc/rc.conf/etc/rc.conf.local/etc/rc.d//etc/rc.firewall/etc/rc.local/etc/rc.shutdown/etc/rc.subr/var/run/dmesg.bootβdmesg(8)results soon after thercprocess begins. Useful whendmesg(8)buffer in the kernel no longer has this information.
π¦ EXAMPLES π¦
The following is a minimal rc.d/ style script. Most scripts require little more than the following.
#!/bin/sh
#
# PROVIDE: foo
# REQUIRE: bar_service_required_to_precede_foo
. /etc/rc.subr
name="foo"
rcvar=foo_enable
command="/usr/local/bin/foo"
load_rc_config $name
run_rc_command "$1"
Certain scripts may want to provide enhanced functionality. The user may access this functionality through additional commands. The script may list and define as many commands at it needs. π
#!/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=foo_enable
command="/usr/local/bin/foo"
extra_commands="nop hello"
hello_cmd="echo Hello World."
nop_cmd="do_nop"
do_nop()
{
echo "I do nothing."
}
load_rc_config $name
run_rc_command "$1"
As all processes are killed by init(8) at shutdown, the explicit kill(1) is unnecessary, but is often included. π
β SEE ALSO β
kill(1)rc.conf(5)init(8)rc.resume(8)rc.subr(8)rcorder(8)reboot(8)savecore(8)service(8)sysrc(8)
Practical rc.d scripting in BSD, <https://docs.freebsd.org/en/articles/rc-scripting/>. π§
π HISTORY π
The rc utility appeared in 4.0BSD. π₯
Source/version and date tea: Debian, September 20, 2024 β RC(8) signing off with maximum sparkle. Byeee π
πβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms