xref: /freebsd/share/man/man8/rc.8 (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 December 27,2023
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 each script in turn using
152.Fn run_rc_script
153(from
154.Xr rc.subr 8 ) ,
155which sets
156.Va $1
157to
158.Dq Li start ,
159and sources the script in a subshell.
160Stop processing when the script that is the value of the
161.Va $early_late_divider
162has been run.
163.It
164Check again to see if the file
165.Va ${firstboot_sentinel}
166exists (in case it is located on a newly mounted file system)
167and adjust the list of KEYWORDs to skip appropriately.
168.It
169Re-run
170.Xr rcorder 8 ,
171this time including the scripts in the
172.Va $local_startup
173directories.
174Ignore everything up to the
175.Va $early_late_divider ,
176then start executing the scripts as described above.
177.It
178If the file
179.Va ${firstboot_sentinel}
180exists, delete it.
181If the file
182.Va ${firstboot_sentinel}-reboot
183also exists (because it was created by a script), then delete it and reboot.
184.El
185.Ss Operation of Nm rc.shutdown
186.Bl -enum
187.It
188Set
189.Va rc_shutdown
190to the value of the first argument passed to
191.Nm rc.shutdown
192or to
193.Dq Li unspecified
194if no argument was passed.
195.It
196Source
197.Pa /etc/rc.subr
198to load various
199.Xr rc.subr 8
200shell functions to use.
201.It
202Load the configuration files.
203.It
204Invoke
205.Xr rcorder 8
206to order the files in
207.Pa /etc/rc.d/
208and the
209.Va $local_startup
210directories
211that have a
212.Dq Li shutdown
213KEYWORD (refer to
214.Xr rcorder 8 Ns 's
215.Fl k
216flag),
217reverse that order, and assign the result to a variable.
218.It
219Call each script in turn using
220.Fn run_rc_script
221(from
222.Xr rc.subr 8 ) ,
223which sets
224.Va $1
225to
226.Dq Li faststop ,
227and sources the script in a subshell.
228.El
229.Ss Contents of Nm rc.d/
230.Nm rc.d/
231is located in
232.Pa /etc/rc.d/ .
233The following file naming conventions are currently used in
234.Nm rc.d/ :
235.Bl -tag -width ".Pa ALLUPPERCASE" -offset indent
236.It Pa ALLUPPERCASE
237Scripts that are
238.Dq placeholders
239to ensure that certain operations are performed before others.
240In order of startup, these are:
241.Bl -tag -width ".Pa FILESYSTEMS"
242.It Pa FILESYSTEMS
243Ensure that root and other critical file systems are mounted.
244This is the default
245.Va $early_late_divider .
246.It Pa NETWORKING
247Ensure basic network services are running, including general
248network configuration.
249.It Pa SERVERS
250Ensure basic services
251exist for services that start early (such as
252.Pa nisdomain ) ,
253because they are required by
254.Pa DAEMON
255below.
256.It Pa DAEMON
257Check-point before all general purpose daemons such as
258.Pa lpd
259and
260.Pa ntpd .
261.It Pa LOGIN
262Check-point before user login services
263.Pa ( inetd
264and
265.Pa sshd ) ,
266as well as services which might run commands as users
267.Pa ( cron
268and
269.Pa sendmail ) .
270.El
271.It Pa bar
272Scripts that are sourced in a subshell.
273The boot does not stop if such a script terminates with a non-zero status,
274but a script can stop the boot if necessary by invoking the
275.Fn stop_boot
276function (from
277.Xr rc.subr 8 ) .
278.El
279.Pp
280Each script should contain
281.Xr rcorder 8
282keywords, especially an appropriate
283.Dq Li PROVIDE
284entry, and if necessary
285.Dq Li REQUIRE
286and
287.Dq Li BEFORE
288keywords.
289.Pp
290Each script is expected to support at least the following arguments, which
291are automatically supported if it uses the
292.Fn run_rc_command
293function:
294.Bl -tag -width ".Cm restart" -offset indent
295.It Cm start
296Start the service.
297This should check that the service is to be started as specified by
298.Xr rc.conf 5 .
299Also checks if the service is already running and refuses to start if
300it is.
301This latter check is not performed by standard
302.Fx
303scripts if the system is starting directly to multi-user mode, to
304speed up the boot process.
305If
306.Cm forcestart
307is given, ignore the
308.Xr rc.conf 5
309check and start anyway.
310.It Cm stop
311If the service is to be started as specified by
312.Xr rc.conf 5 ,
313stop the service.
314This should check that the service is running and complain if it is not.
315If
316.Cm forcestop
317is given, ignore the
318.Xr rc.conf 5
319check and attempt to stop.
320.It Cm restart
321Perform a
322.Cm stop
323then a
324.Cm start .
325.It Cm status
326If the script starts a process (rather than performing a one-off
327operation), show the status of the process.
328Otherwise it is not necessary to support this argument.
329Defaults to displaying the process ID of the program (if running).
330.It Cm enable
331Enable the service in
332.Xr rc.conf 5 .
333.It Cm disable
334Disable the service in
335.Xr rc.conf 5 .
336.It Cm delete
337Remove the service from
338.Xr rc.conf 5 .
339If
340.Ql Li service_delete_empty
341is set to
342.Dq Li YES ,
343.Pa /etc/rc.conf.d/$servicename
344will be deleted if empty after modification.
345.It Cm describe
346Print a short description of what the script does.
347.It Cm extracommands
348Print the script's non-standard commands.
349.It Cm poll
350If the script starts a process (rather than performing a one-off
351operation), wait for the command to exit.
352Otherwise it is not necessary to support this argument.
353.It Cm enabled
354Return 0 if the service is enabled and 1 if it is not.
355This command does not print anything.
356.It Cm rcvar
357Display which
358.Xr rc.conf 5
359variables are used to control the startup of the service (if any).
360.El
361.Pp
362If a script must implement additional commands it can list them in
363the
364.Va extra_commands
365variable, and define their actions in a variable constructed from
366the command name (see the
367.Sx EXAMPLES
368section).
369.Pp
370The configuration files are normally read only once at the start of a boot
371sequence; if a script needs to
372.Cm enable
373or
374.Cm disable
375any other script that would run later in the sequence, it must send a
376.Dv SIGALRM
377to the rc process (identified by
378.Ev $RC_PID )
379to have it re-read the files.
380.Pp
381The following key points apply to old-style scripts in
382.Pa /usr/local/etc/rc.d/ :
383.Bl -bullet
384.It
385Scripts are only executed if their
386.Xr basename 1
387matches the shell globbing pattern
388.Pa *.sh ,
389and they are executable.
390Any other files or directories present within the directory are silently
391ignored.
392.It
393When a script is executed at boot time, it is passed the string
394.Dq Li start
395as its first and only argument.
396At shutdown time, it is passed the string
397.Dq Li stop
398as its first and only argument.
399All
400.Nm rc.d/
401scripts are expected to handle these arguments appropriately.
402If no action needs to be taken at a given time
403(either boot time or shutdown time),
404the script should exit successfully and without producing an error message.
405.It
406The scripts within each directory are executed in lexicographical order.
407If a specific order is required,
408numbers may be used as a prefix to the existing filenames,
409so for example
410.Pa 100.foo
411would be executed before
412.Pa 200.bar ;
413without the numeric prefixes the opposite would be true.
414.It
415The output from each script is traditionally a space character,
416followed by the name of the software package being started or shut down,
417.Em without
418a trailing newline character.
419.El
420.Sh SCRIPTS OF INTEREST
421When an automatic reboot is in progress,
422.Nm
423is invoked with the argument
424.Cm autoboot .
425One of the scripts run from
426.Pa /etc/rc.d/
427is
428.Pa /etc/rc.d/fsck .
429This script runs
430.Xr fsck 8
431with option
432.Fl p
433and
434.Fl F
435to
436.Dq preen
437all the disks of minor inconsistencies resulting
438from the last system shutdown.
439If this fails, then checks/repairs of serious inconsistencies
440caused by hardware or software failure will be performed
441in the background at the end of the booting process.
442If
443.Cm autoboot
444is not set, when going from single-user to multi-user mode for example,
445the script does not do anything.
446.Pp
447The
448.Pa /etc/rc.d/local
449script can execute scripts from multiple
450.Nm rc.d/
451directories.
452The default location includes
453.Pa /usr/local/etc/rc.d/ ,
454but these may be overridden with the
455.Va local_startup
456.Xr rc.conf 5
457variable.
458.Pp
459The
460.Pa /etc/rc.d/serial
461script is used to set any special configurations for serial devices.
462.Pp
463The
464.Nm rc.firewall
465script is used to configure rules for the kernel based firewall
466service.
467It has several possible options:
468.Pp
469.Bl -tag -width ".Ar filename" -compact -offset indent
470.It Cm open
471will allow anyone in
472.It Cm client
473will try to protect just this machine
474.It Cm simple
475will try to protect a whole network
476.It Cm closed
477totally disables IP services except via
478.Pa lo0
479interface
480.It Cm UNKNOWN
481disables the loading of firewall rules
482.It Ar filename
483will load the rules in the given filename (full path required).
484.El
485.Pp
486Most daemons, including network related daemons, have their own script in
487.Pa /etc/rc.d/ ,
488which can be used to start, stop, and check the status of the service.
489.Pp
490Any architecture specific scripts, such as
491.Pa /etc/rc.d/apm
492for example, specifically check that they are on that architecture
493before starting the daemon.
494.Pp
495Following tradition, all startup files reside in
496.Pa /etc .
497.Sh FILES
498.Bl -tag -compact -width Pa
499.It Pa /etc/rc
500.It Pa /etc/rc.conf
501.It Pa /etc/rc.conf.local
502.It Pa /etc/rc.d/
503.It Pa /etc/rc.firewall
504.It Pa /etc/rc.local
505.It Pa /etc/rc.shutdown
506.It Pa /etc/rc.subr
507.It Pa /var/run/dmesg.boot
508.Xr dmesg 8
509results soon after the
510.Nm
511process begins.
512Useful when
513.Xr dmesg 8
514buffer in the kernel no longer has this information.
515.El
516.Sh EXAMPLES
517The following is a minimal
518.Nm rc.d/
519style script.
520Most scripts require little more than the following.
521.Bd -literal -offset indent
522#!/bin/sh
523#
524
525# PROVIDE: foo
526# REQUIRE: bar_service_required_to_precede_foo
527
528\&. /etc/rc.subr
529
530name="foo"
531rcvar=foo_enable
532command="/usr/local/bin/foo"
533
534load_rc_config $name
535run_rc_command "$1"
536.Ed
537.Pp
538Certain scripts may want to provide enhanced functionality.
539The user may access this functionality through additional commands.
540The script may list and define as many commands at it needs.
541.Bd -literal -offset indent
542#!/bin/sh
543#
544
545# PROVIDE: foo
546# REQUIRE: bar_service_required_to_precede_foo
547# BEFORE:  baz_service_requiring_foo_to_precede_it
548
549\&. /etc/rc.subr
550
551name="foo"
552rcvar=foo_enable
553command="/usr/local/bin/foo"
554extra_commands="nop hello"
555hello_cmd="echo Hello World."
556nop_cmd="do_nop"
557
558do_nop()
559{
560	echo "I do nothing."
561}
562
563load_rc_config $name
564run_rc_command "$1"
565.Ed
566.Pp
567As all processes are killed by
568.Xr init 8
569at shutdown, the explicit
570.Xr kill 1
571is unnecessary, but is often included.
572.Sh SEE ALSO
573.Xr kill 1 ,
574.Xr rc.conf 5 ,
575.Xr init 8 ,
576.Xr rc.resume 8 ,
577.Xr rc.subr 8 ,
578.Xr rcorder 8 ,
579.Xr reboot 8 ,
580.Xr savecore 8 ,
581.Xr service 8 ,
582.Xr sysrc 8
583.Pp
584.Rs
585.%T Practical rc.d scripting in BSD
586.%U <https://docs.freebsd.org/en/articles/rc-scripting/>
587.Re
588.Sh HISTORY
589The
590.Nm
591utility appeared in
592.Bx 4.0 .
593