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