xref: /freebsd/share/man/man8/rc.8 (revision 7815283df299be63807225a9fe9b6e54406eae28)
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 April 25, 2017
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 sysrc(8) command provides a scripting interface to modify system
93config files.
94.Ss Operation of Nm
95.Bl -enum
96.It
97If autobooting, set
98.Va autoboot Ns = Ns Li yes
99and enable a flag
100.Pq Va rc_fast Ns = Ns Li yes ,
101which prevents the
102.Nm rc.d/
103scripts from performing the check for already running processes
104(thus speeding up the boot process).
105This
106.Va rc_fast Ns = Ns Li yes
107speedup will not occur when
108.Nm
109is started up after exiting the single-user shell.
110.It
111Determine whether the system is booting diskless,
112and if so run the
113.Pa /etc/rc.initdiskless
114script.
115.It
116Source
117.Pa /etc/rc.subr
118to load various
119.Xr rc.subr 8
120shell functions to use.
121.It
122Load the configuration files.
123.It
124Determine if booting in a jail,
125and add
126.Dq Li nojail
127(no jails allowed) or
128.Dq Li nojailvnet
129(only allow vnet-enabled jails) to the list of KEYWORDS to skip in
130.Xr rcorder 8 .
131.It
132If the file
133.Va ${firstboot_sentinel}
134does not exist, add
135.Dq Li firstboot
136to the list of KEYWORDS to skip in
137.Xr rcorder 8 .
138.It
139Invoke
140.Xr rcorder 8
141to order the files in
142.Pa /etc/rc.d/
143that do not have a
144.Dq Li nostart
145KEYWORD (refer to
146.Xr rcorder 8 Ns 's
147.Fl s
148flag).
149.It
150Call each script in turn using
151.Fn run_rc_script
152(from
153.Xr rc.subr 8 ) ,
154which sets
155.Va $1
156to
157.Dq Li start ,
158and sources the script in a subshell.
159If the script has a
160.Pa .sh
161suffix then it is sourced directly into the current shell.
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
190Source
191.Pa /etc/rc.subr
192to load various
193.Xr rc.subr 8
194shell functions to use.
195.It
196Load the configuration files.
197.It
198Invoke
199.Xr rcorder 8
200to order the files in
201.Pa /etc/rc.d/
202and the
203.Va $local_startup
204directories
205that have a
206.Dq Li shutdown
207KEYWORD (refer to
208.Xr rcorder 8 Ns 's
209.Fl k
210flag),
211reverse that order, and assign the result to a variable.
212.It
213Call each script in turn using
214.Fn run_rc_script
215(from
216.Xr rc.subr 8 ) ,
217which sets
218.Va $1
219to
220.Dq Li stop ,
221and sources the script in a subshell.
222If the script has a
223.Pa .sh
224suffix then it is sourced directly into the current shell.
225.El
226.Ss Contents of Nm rc.d/
227.Nm rc.d/
228is located in
229.Pa /etc/rc.d/ .
230The following file naming conventions are currently used in
231.Nm rc.d/ :
232.Bl -tag -width ".Pa ALLUPPERCASE" -offset indent
233.It Pa ALLUPPERCASE
234Scripts that are
235.Dq placeholders
236to ensure that certain operations are performed before others.
237In order of startup, these are:
238.Bl -tag -width ".Pa NETWORKING"
239.It Pa NETWORKING
240Ensure basic network services are running, including general
241network configuration.
242.It Pa SERVERS
243Ensure basic services
244exist for services that start early (such as
245.Pa nisdomain ) ,
246because they are required by
247.Pa DAEMON
248below.
249.It Pa DAEMON
250Check-point before all general purpose daemons such as
251.Pa lpd
252and
253.Pa ntpd .
254.It Pa LOGIN
255Check-point before user login services
256.Pa ( inetd
257and
258.Pa sshd ) ,
259as well as services which might run commands as users
260.Pa ( cron
261and
262.Pa sendmail ) .
263.El
264.It Pa foo.sh
265Scripts that are to be sourced into the current shell rather than a subshell
266have a
267.Pa .sh
268suffix.
269Extreme care must be taken in using this, as the startup sequence will
270terminate if the script does.
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 describe
331Print a short description of what the script does.
332.It Cm extracommands
333Print the script's non-standard commands.
334.It Cm poll
335If the script starts a process (rather than performing a one-off
336operation), wait for the command to exit.
337Otherwise it is not necessary to support this argument.
338.It Cm enabled
339Return 0 if the service is enabled and 1 if it is not.
340This command does not print anything.
341.It Cm rcvar
342Display which
343.Xr rc.conf 5
344variables are used to control the startup of the service (if any).
345.El
346.Pp
347If a script must implement additional commands it can list them in
348the
349.Va extra_commands
350variable, and define their actions in a variable constructed from
351the command name (see the
352.Sx EXAMPLES
353section).
354.Pp
355The following key points apply to old-style scripts in
356.Pa /usr/local/etc/rc.d/ :
357.Bl -bullet
358.It
359Scripts are only executed if their
360.Xr basename 1
361matches the shell globbing pattern
362.Pa *.sh ,
363and they are executable.
364Any other files or directories present within the directory are silently
365ignored.
366.It
367When a script is executed at boot time, it is passed the string
368.Dq Li start
369as its first and only argument.
370At shutdown time, it is passed the string
371.Dq Li stop
372as its first and only argument.
373All
374.Nm rc.d/
375scripts are expected to handle these arguments appropriately.
376If no action needs to be taken at a given time
377(either boot time or shutdown time),
378the script should exit successfully and without producing an error message.
379.It
380The scripts within each directory are executed in lexicographical order.
381If a specific order is required,
382numbers may be used as a prefix to the existing filenames,
383so for example
384.Pa 100.foo
385would be executed before
386.Pa 200.bar ;
387without the numeric prefixes the opposite would be true.
388.It
389The output from each script is traditionally a space character,
390followed by the name of the software package being started or shut down,
391.Em without
392a trailing newline character (see the
393.Sx EXAMPLES
394section).
395.El
396.Sh SCRIPTS OF INTEREST
397When an automatic reboot is in progress,
398.Nm
399is invoked with the argument
400.Cm autoboot .
401One of the scripts run from
402.Pa /etc/rc.d/
403is
404.Pa /etc/rc.d/fsck .
405This script runs
406.Xr fsck 8
407with option
408.Fl p
409and
410.Fl F
411to
412.Dq preen
413all the disks of minor inconsistencies resulting
414from the last system shutdown.
415If this fails, then checks/repairs of serious inconsistencies
416caused by hardware or software failure will be performed
417in the background at the end of the booting process.
418If
419.Cm autoboot
420is not set, when going from single-user to multi-user mode for example,
421the script does not do anything.
422.Pp
423The
424.Pa /etc/rc.d/local
425script can execute scripts from multiple
426.Nm rc.d/
427directories.
428The default location includes
429.Pa /usr/local/etc/rc.d/ ,
430but these may be overridden with the
431.Va local_startup
432.Xr rc.conf 5
433variable.
434.Pp
435The
436.Pa /etc/rc.d/serial
437script is used to set any special configurations for serial devices.
438.Pp
439The
440.Nm rc.firewall
441script is used to configure rules for the kernel based firewall
442service.
443It has several possible options:
444.Pp
445.Bl -tag -width ".Ar filename" -compact -offset indent
446.It Cm open
447will allow anyone in
448.It Cm client
449will try to protect just this machine
450.It Cm simple
451will try to protect a whole network
452.It Cm closed
453totally disables IP services except via
454.Pa lo0
455interface
456.It Cm UNKNOWN
457disables the loading of firewall rules
458.It Ar filename
459will load the rules in the given filename (full path required).
460.El
461.Pp
462Most daemons, including network related daemons, have their own script in
463.Pa /etc/rc.d/ ,
464which can be used to start, stop, and check the status of the service.
465.Pp
466Any architecture specific scripts, such as
467.Pa /etc/rc.d/apm
468for example, specifically check that they are on that architecture
469before starting the daemon.
470.Pp
471Following tradition, all startup files reside in
472.Pa /etc .
473.Sh FILES
474.Bl -tag -compact -width Pa
475.It Pa /etc/rc
476.It Pa /etc/rc.conf
477.It Pa /etc/rc.conf.local
478.It Pa /etc/rc.d/
479.It Pa /etc/rc.firewall
480.It Pa /etc/rc.local
481.It Pa /etc/rc.shutdown
482.It Pa /etc/rc.subr
483.It Pa /var/run/dmesg.boot
484.Xr dmesg 8
485results soon after the
486.Nm
487process begins.
488Useful when
489.Xr dmesg 8
490buffer in the kernel no longer has this information.
491.El
492.Sh EXAMPLES
493The following is a minimal
494.Nm rc.d/
495style script.
496Most scripts require little more than the following.
497.Bd -literal -offset indent
498#!/bin/sh
499#
500
501# PROVIDE: foo
502# REQUIRE: bar_service_required_to_precede_foo
503
504\&. /etc/rc.subr
505
506name="foo"
507rcvar=foo_enable
508command="/usr/local/bin/foo"
509
510load_rc_config $name
511run_rc_command "$1"
512.Ed
513.Pp
514Certain scripts may want to provide enhanced functionality.
515The user may access this functionality through additional commands.
516The script may list and define as many commands at it needs.
517.Bd -literal -offset indent
518#!/bin/sh
519#
520
521# PROVIDE: foo
522# REQUIRE: bar_service_required_to_precede_foo
523# BEFORE:  baz_service_requiring_foo_to_precede_it
524
525\&. /etc/rc.subr
526
527name="foo"
528rcvar=foo_enable
529command="/usr/local/bin/foo"
530extra_commands="nop hello"
531hello_cmd="echo Hello World."
532nop_cmd="do_nop"
533
534do_nop()
535{
536	echo "I do nothing."
537}
538
539load_rc_config $name
540run_rc_command "$1"
541.Ed
542.Pp
543As all processes are killed by
544.Xr init 8
545at shutdown, the explicit
546.Xr kill 1
547is unnecessary, but is often included.
548.Sh SEE ALSO
549.Xr kill 1 ,
550.Xr rc.conf 5 ,
551.Xr init 8 ,
552.Xr rc.resume 8 ,
553.Xr rc.subr 8 ,
554.Xr rcorder 8 ,
555.Xr reboot 8 ,
556.Xr savecore 8 ,
557.Xr sysrc 8
558.Sh HISTORY
559The
560.Nm
561utility appeared in
562.Bx 4.0 .
563