xref: /freebsd/share/man/man8/rc.8 (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Portions of this manual page are Copyrighted by
5.\"	The NetBSD Foundation.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd September 20, 2024
32.Dt RC 8
33.Os
34.Sh NAME
35.Nm rc
36.Nd command scripts for auto-reboot and daemon startup
37.Sh SYNOPSIS
38.Nm
39.Nm rc.conf
40.Nm rc.conf.local
41.Nm rc.d/
42.Nm rc.firewall
43.Nm rc.local
44.Nm rc.resume
45.Nm rc.shutdown
46.Nm rc.subr
47.Sh DESCRIPTION
48The
49.Nm
50utility is the command script which controls the automatic boot process
51after being called by
52.Xr init 8 .
53The
54.Nm rc.local
55script contains commands which are pertinent only
56to a specific site.
57Typically, the
58.Pa /usr/local/etc/rc.d/
59mechanism is used instead of
60.Nm rc.local
61these days but if
62you want to use
63.Nm rc.local ,
64it is still supported.
65In this case, it should source
66.Pa /etc/rc.conf
67and contain additional custom startup code for your system.
68The best way to handle
69.Nm rc.local ,
70however, is to separate it out into
71.Nm rc.d/
72style scripts and place them under
73.Pa /usr/local/etc/rc.d/ .
74The
75.Nm rc.conf
76file contains the global system configuration information referenced
77by the startup scripts, while
78.Nm rc.conf.local
79contains the local system configuration.
80See
81.Xr rc.conf 5
82for more information.
83.Pp
84The
85.Nm rc.d/
86directories contain scripts which will be automatically
87executed at boot time and shutdown time.
88.Pp
89The
90.Xr service 8
91command provides a convenient interface to manage rc.d services.
92.Pp
93The
94.Xr sysrc 8
95command provides a scripting interface to modify system config files.
96.Ss Operation of Nm
97.Bl -enum
98.It
99If autobooting, set
100.Va autoboot Ns = Ns Li yes
101and enable a flag
102.Pq Va rc_fast Ns = Ns Li yes ,
103which prevents the
104.Nm rc.d/
105scripts from performing the check for already running processes
106(thus speeding up the boot process).
107This
108.Va rc_fast Ns = Ns Li yes
109speedup will not occur when
110.Nm
111is started up after exiting the single-user shell.
112.It
113Determine whether the system is booting diskless,
114and if so run the
115.Pa /etc/rc.initdiskless
116script.
117.It
118Source
119.Pa /etc/rc.subr
120to load various
121.Xr rc.subr 8
122shell functions to use.
123.It
124Load the configuration files (see below for reloading).
125.It
126Determine if booting in a jail, and add
127.Dq Li nojail
128(no jails allowed) or
129.Dq Li nojailvnet
130(only allow vnet-enabled jails) to the list of KEYWORDS to skip in
131.Xr rcorder 8 .
132.It
133If the file
134.Va ${firstboot_sentinel}
135does not exist, add
136.Dq Li firstboot
137to the list of KEYWORDS to skip in
138.Xr rcorder 8 .
139.It
140Invoke
141.Xr rcorder 8
142to order the files in
143.Pa /etc/rc.d/
144that do not have a
145.Dq Li nostart
146KEYWORD (refer to
147.Xr rcorder 8 Ns 's
148.Fl s
149flag).
150.It
151Call
152.Fn run_rc_scripts
153with the list of scripts to be run.
154.Pp
155For each script that will call
156.Fn run_rc_script
157(both functions are from
158.Xr rc.subr 8 ) ,
159which sets
160.Va $1
161to
162.Dq Li start ,
163and sources the script in a subshell.
164Stop processing when the script that is the value of the
165.Va $early_late_divider
166has been run.
167.It
168Check again to see if the file
169.Va ${firstboot_sentinel}
170exists (in case it is located on a newly mounted file system)
171and adjust the list of KEYWORDs to skip appropriately.
172.It
173Re-run
174.Xr rcorder 8 ,
175this time including the scripts in the
176.Va $local_startup
177directories.
178Call
179.Fn run_rc_scripts
180again with the new list of scripts.
181It will skip any that have already been run and
182execute the rest as described above.
183.It
184If the file
185.Va ${firstboot_sentinel}
186exists, delete it.
187If the file
188.Va ${firstboot_sentinel}-reboot
189also exists (because it was created by a script), then delete it and reboot.
190.El
191.Ss Operation of Nm rc.shutdown
192.Bl -enum
193.It
194Set
195.Va rc_shutdown
196to the value of the first argument passed to
197.Nm rc.shutdown
198or to
199.Dq Li unspecified
200if no argument was passed.
201.It
202Source
203.Pa /etc/rc.subr
204to load various
205.Xr rc.subr 8
206shell functions to use.
207.It
208Load the configuration files.
209.It
210Invoke
211.Xr rcorder 8
212to order the files in
213.Pa /etc/rc.d/
214and the
215.Va $local_startup
216directories
217that have a
218.Dq Li shutdown
219KEYWORD (refer to
220.Xr rcorder 8 Ns 's
221.Fl k
222flag),
223reverse that order, and assign the result to a variable.
224.It
225Call each script in turn using
226.Fn run_rc_script
227(from
228.Xr rc.subr 8 ) ,
229which sets
230.Va $1
231to
232.Dq Li faststop ,
233and sources the script in a subshell.
234.El
235.Ss Contents of Nm rc.d/
236.Nm rc.d/
237is located in
238.Pa /etc/rc.d/ .
239The following file naming conventions are currently used in
240.Nm rc.d/ :
241.Bl -tag -width ".Pa ALLUPPERCASE" -offset indent
242.It Pa ALLUPPERCASE
243Scripts that are
244.Dq placeholders
245to ensure that certain operations are performed before others.
246In order of startup, these are:
247.Bl -tag -width ".Pa FILESYSTEMS"
248.It Pa FILESYSTEMS
249Ensure that root and other critical file systems are mounted.
250This is the default
251.Va $early_late_divider .
252.It Pa NETWORKING
253Ensure basic network services are running, including general
254network configuration.
255.It Pa SERVERS
256Ensure basic services
257exist for services that start early (such as
258.Pa nisdomain ) ,
259because they are required by
260.Pa DAEMON
261below.
262.It Pa DAEMON
263Check-point before all general purpose daemons such as
264.Pa lpd
265and
266.Pa ntpd .
267.It Pa LOGIN
268Check-point before user login services
269.Pa ( inetd
270and
271.Pa sshd ) ,
272as well as services which might run commands as users
273.Pa ( cron
274and
275.Pa sendmail ) .
276.El
277.It Pa bar
278Scripts that are sourced in a subshell.
279The boot does not stop if such a script terminates with a non-zero status,
280but a script can stop the boot if necessary by invoking the
281.Fn stop_boot
282function (from
283.Xr rc.subr 8 ) .
284.El
285.Pp
286Each script should contain
287.Xr rcorder 8
288keywords, especially an appropriate
289.Dq Li PROVIDE
290entry, and if necessary
291.Dq Li REQUIRE
292and
293.Dq Li BEFORE
294keywords.
295.Pp
296Each script is expected to support at least the following arguments, which
297are automatically supported if it uses the
298.Fn run_rc_command
299function:
300.Bl -tag -width ".Cm restart" -offset indent
301.It Cm start
302Start the service.
303This should check that the service is to be started as specified by
304.Xr rc.conf 5 .
305Also checks if the service is already running and refuses to start if
306it is.
307This latter check is not performed by standard
308.Fx
309scripts if the system is starting directly to multi-user mode, to
310speed up the boot process.
311If
312.Cm forcestart
313is given, ignore the
314.Xr rc.conf 5
315check and start anyway.
316.It Cm stop
317If the service is to be started as specified by
318.Xr rc.conf 5 ,
319stop the service.
320This should check that the service is running and complain if it is not.
321If
322.Cm forcestop
323is given, ignore the
324.Xr rc.conf 5
325check and attempt to stop.
326.It Cm restart
327Perform a
328.Cm stop
329then a
330.Cm start .
331.It Cm status
332If the script starts a process (rather than performing a one-off
333operation), show the status of the process.
334Otherwise it is not necessary to support this argument.
335Defaults to displaying the process ID of the program (if running).
336.It Cm enable
337Enable the service in
338.Xr rc.conf 5 .
339.It Cm disable
340Disable the service in
341.Xr rc.conf 5 .
342.It Cm delete
343Remove the service from
344.Xr rc.conf 5 .
345If
346.Ql Li service_delete_empty
347is set to
348.Dq Li YES ,
349.Pa /etc/rc.conf.d/$servicename
350will be deleted if empty after modification.
351.It Cm describe
352Print a short description of what the script does.
353.It Cm extracommands
354Print the script's non-standard commands.
355.It Cm poll
356If the script starts a process (rather than performing a one-off
357operation), wait for the command to exit.
358Otherwise it is not necessary to support this argument.
359.It Cm enabled
360Return 0 if the service is enabled and 1 if it is not.
361This command does not print anything.
362.It Cm rcvar
363Display which
364.Xr rc.conf 5
365variables are used to control the startup of the service (if any).
366.El
367.Pp
368If a script must implement additional commands it can list them in
369the
370.Va extra_commands
371variable, and define their actions in a variable constructed from
372the command name (see the
373.Sx EXAMPLES
374section).
375.Pp
376The configuration files are normally read only once at the start of a boot
377sequence; if a script needs to
378.Cm enable
379or
380.Cm disable
381any other script that would run later in the sequence, it must send a
382.Dv SIGALRM
383to the rc process (identified by
384.Ev $RC_PID )
385to have it re-read the files.
386.Pp
387The following key points apply to old-style scripts in
388.Pa /usr/local/etc/rc.d/ :
389.Bl -bullet
390.It
391Scripts are only executed if their
392.Xr basename 1
393matches the shell globbing pattern
394.Pa *.sh ,
395and they are executable.
396Any other files or directories present within the directory are silently
397ignored.
398.It
399When a script is executed at boot time, it is passed the string
400.Dq Li start
401as its first and only argument.
402At shutdown time, it is passed the string
403.Dq Li stop
404as its first and only argument.
405All
406.Nm rc.d/
407scripts are expected to handle these arguments appropriately.
408If no action needs to be taken at a given time
409(either boot time or shutdown time),
410the script should exit successfully and without producing an error message.
411.It
412The scripts within each directory are executed in lexicographical order.
413If a specific order is required,
414numbers may be used as a prefix to the existing filenames,
415so for example
416.Pa 100.foo
417would be executed before
418.Pa 200.bar ;
419without the numeric prefixes the opposite would be true.
420.It
421The output from each script is traditionally a space character,
422followed by the name of the software package being started or shut down,
423.Em without
424a trailing newline character.
425.El
426.Sh SCRIPTS OF INTEREST
427When an automatic reboot is in progress,
428.Nm
429is invoked with the argument
430.Cm autoboot .
431One of the scripts run from
432.Pa /etc/rc.d/
433is
434.Pa /etc/rc.d/fsck .
435This script runs
436.Xr fsck 8
437with option
438.Fl p
439and
440.Fl F
441to
442.Dq preen
443all the disks of minor inconsistencies resulting
444from the last system shutdown.
445If this fails, then checks/repairs of serious inconsistencies
446caused by hardware or software failure will be performed
447in the background at the end of the booting process.
448If
449.Cm autoboot
450is not set, when going from single-user to multi-user mode for example,
451the script does not do anything.
452.Pp
453The
454.Pa /etc/rc.d/serial
455script is used to set any special configurations for serial devices.
456.Pp
457The
458.Nm rc.firewall
459script is used to configure rules for the kernel based firewall
460service.
461It has several possible options:
462.Pp
463.Bl -tag -width ".Ar filename" -compact -offset indent
464.It Cm open
465will allow anyone in
466.It Cm client
467will try to protect just this machine
468.It Cm simple
469will try to protect a whole network
470.It Cm closed
471totally disables IP services except via
472.Pa lo0
473interface
474.It Cm UNKNOWN
475disables the loading of firewall rules
476.It Ar filename
477will load the rules in the given filename (full path required).
478.El
479.Pp
480Most daemons, including network related daemons, have their own script in
481.Pa /etc/rc.d/ ,
482which can be used to start, stop, and check the status of the service.
483.Pp
484Any architecture specific scripts, such as
485.Pa /etc/rc.d/apm
486for example, specifically check that they are on that architecture
487before starting the daemon.
488.Pp
489Following tradition, all startup files reside in
490.Pa /etc .
491.Sh FILES
492.Bl -tag -compact -width Pa
493.It Pa /etc/rc
494.It Pa /etc/rc.conf
495.It Pa /etc/rc.conf.local
496.It Pa /etc/rc.d/
497.It Pa /etc/rc.firewall
498.It Pa /etc/rc.local
499.It Pa /etc/rc.shutdown
500.It Pa /etc/rc.subr
501.It Pa /var/run/dmesg.boot
502.Xr dmesg 8
503results soon after the
504.Nm
505process begins.
506Useful when
507.Xr dmesg 8
508buffer in the kernel no longer has this information.
509.El
510.Sh EXAMPLES
511The following is a minimal
512.Nm rc.d/
513style script.
514Most scripts require little more than the following.
515.Bd -literal -offset indent
516#!/bin/sh
517#
518
519# PROVIDE: foo
520# REQUIRE: bar_service_required_to_precede_foo
521
522\&. /etc/rc.subr
523
524name="foo"
525rcvar=foo_enable
526command="/usr/local/bin/foo"
527
528load_rc_config $name
529run_rc_command "$1"
530.Ed
531.Pp
532Certain scripts may want to provide enhanced functionality.
533The user may access this functionality through additional commands.
534The script may list and define as many commands at it needs.
535.Bd -literal -offset indent
536#!/bin/sh
537#
538
539# PROVIDE: foo
540# REQUIRE: bar_service_required_to_precede_foo
541# BEFORE:  baz_service_requiring_foo_to_precede_it
542
543\&. /etc/rc.subr
544
545name="foo"
546rcvar=foo_enable
547command="/usr/local/bin/foo"
548extra_commands="nop hello"
549hello_cmd="echo Hello World."
550nop_cmd="do_nop"
551
552do_nop()
553{
554	echo "I do nothing."
555}
556
557load_rc_config $name
558run_rc_command "$1"
559.Ed
560.Pp
561As all processes are killed by
562.Xr init 8
563at shutdown, the explicit
564.Xr kill 1
565is unnecessary, but is often included.
566.Sh SEE ALSO
567.Xr kill 1 ,
568.Xr rc.conf 5 ,
569.Xr init 8 ,
570.Xr rc.resume 8 ,
571.Xr rc.subr 8 ,
572.Xr rcorder 8 ,
573.Xr reboot 8 ,
574.Xr savecore 8 ,
575.Xr service 8 ,
576.Xr sysrc 8
577.Pp
578.Rs
579.%T Practical rc.d scripting in BSD
580.%U <https://docs.freebsd.org/en/articles/rc-scripting/>
581.Re
582.Sh HISTORY
583The
584.Nm
585utility appeared in
586.Bx 4.0 .
587