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