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