xref: /freebsd/share/man/man8/rc.8 (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)rc.8	8.2 (Berkeley) 12/11/93
36.\" $FreeBSD$
37.\"
38.Dd December 19, 2005
39.Dt RC 8
40.Os
41.Sh NAME
42.Nm rc
43.Nd command scripts for auto-reboot and daemon startup
44.Sh SYNOPSIS
45.Nm
46.Nm rc.conf
47.Nm rc.conf.local
48.Nm rc.d/
49.Nm rc.firewall
50.Nm rc.local
51.Nm rc.shutdown
52.Nm rc.subr
53.Sh DESCRIPTION
54The
55.Nm
56utility is the command script which controls the automatic boot process
57after being called by
58.Xr init 8 .
59The
60.Nm rc.local
61script contains commands which are pertinent only
62to a specific site.
63Typically, the
64.Pa /usr/local/etc/rc.d/
65mechanism is used instead of
66.Nm rc.local
67these days but if
68you want to use
69.Nm rc.local ,
70it is still supported.
71In this case, it should source
72.Pa /etc/rc.conf
73and contain additional custom startup code for your system.
74The best way to handle
75.Nm rc.local ,
76however, is to separate it out into
77.Nm rc.d/
78style scripts and place them under
79.Pa /usr/local/etc/rc.d/ .
80The
81.Nm rc.conf
82file contains the global system configuration information referenced
83by the startup scripts, while
84.Nm rc.conf.local
85contains the local system configuration.
86See
87.Xr rc.conf 5
88for more information.
89.Pp
90The
91.Nm rc.d/
92directories contain scripts which will be automatically
93executed at boot time and shutdown time.
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 nojail
127to the list of KEYWORDS to skip in
128.Xr rcorder 8 .
129.It
130Invoke
131.Xr rcorder 8
132to order the files in
133.Pa /etc/rc.d/
134that do not have a
135.Dq Li nostart
136KEYWORD (refer to
137.Xr rcorder 8 Ns 's
138.Fl s
139flag).
140.It
141Call each script in turn using
142.Fn run_rc_script
143(from
144.Xr rc.subr 8 ) ,
145which sets
146.Va $1
147to
148.Dq Li start ,
149and sources the script in a subshell.
150If the script has a
151.Pa .sh
152suffix then it is sourced directly into the current shell.
153Stop processing when the script that is the value of the
154.Va $early_late_divider
155has been run.
156.It
157Re-run
158.Xr rcorder 8 ,
159this time including the scripts in the
160.Va $local_startup
161directories.
162Ignore everything up to the
163.Va $early_late_divider ,
164then start executing the scripts as described above.
165.El
166.Ss Operation of Nm rc.shutdown
167.Bl -enum
168.It
169Source
170.Pa /etc/rc.subr
171to load various
172.Xr rc.subr 8
173shell functions to use.
174.It
175Load the configuration files.
176.It
177Invoke
178.Xr rcorder 8
179to order the files in
180.Pa /etc/rc.d/
181and the
182.Va $local_startup
183directories
184that have a
185.Dq Li shutdown
186KEYWORD (refer to
187.Xr rcorder 8 Ns 's
188.Fl k
189flag),
190reverse that order, and assign the result to a variable.
191.It
192Call each script in turn using
193.Fn run_rc_script
194(from
195.Xr rc.subr 8 ) ,
196which sets
197.Va $1
198to
199.Dq Li stop ,
200and sources the script in a subshell.
201If the script has a
202.Pa .sh
203suffix then it is sourced directly into the current shell.
204.El
205.Ss Contents of Nm rc.d/
206.Nm rc.d/
207is located in
208.Pa /etc/rc.d/ .
209The following file naming conventions are currently used in
210.Nm rc.d/ :
211.Bl -tag -width ".Pa ALLUPPERCASE" -offset indent
212.It Pa ALLUPPERCASE
213Scripts that are
214.Dq placeholders
215to ensure that certain operations are performed before others.
216In order of startup, these are:
217.Bl -tag -width ".Pa NETWORKING"
218.It Pa NETWORKING
219Ensure basic network services are running, including general
220network configuration.
221.It Pa SERVERS
222Ensure basic services
223exist for services that start early (such as
224.Pa named ) ,
225because they are required by
226.Pa DAEMON
227below.
228.It Pa DAEMON
229Check-point before all general purpose daemons such as
230.Pa lpd
231and
232.Pa ntpd .
233.It Pa LOGIN
234Check-point before user login services
235.Pa ( inetd
236and
237.Pa sshd ) ,
238as well as services which might run commands as users
239.Pa ( cron
240and
241.Pa sendmail ) .
242.El
243.It Pa foo.sh
244Scripts that are to be sourced into the current shell rather than a subshell
245have a
246.Pa .sh
247suffix.
248Extreme care must be taken in using this, as the startup sequence will
249terminate if the script does.
250.It Pa bar
251Scripts that are sourced in a subshell.
252These can stop the boot if necessary with the following shell
253commands:
254.Bd -literal -offset indent
255if [ "$autoboot" = yes ]; then
256	kill -TERM $$
257fi
258exit 1
259.Ed
260.Pp
261Note that this should be used extremely sparingly!
262.El
263.Pp
264Each script should contain
265.Xr rcorder 8
266keywords, especially an appropriate
267.Dq Li PROVIDE
268entry, and if necessary
269.Dq Li REQUIRE
270and
271.Dq Li BEFORE
272keywords.
273.Pp
274Each script is expected to support at least the following arguments, which
275are automatically supported if it uses the
276.Fn run_rc_command
277function:
278.Bl -tag -width ".Cm restart" -offset indent
279.It Cm start
280Start the service.
281This should check that the service is to be started as specified by
282.Xr rc.conf 5 .
283Also checks if the service is already running and refuses to start if
284it is.
285This latter check is not performed by standard
286.Fx
287scripts if the system is starting directly to multi-user mode, to
288speed up the boot process.
289If
290.Cm forcestart
291is given, ignore the
292.Xr rc.conf 5
293check and start anyway.
294.It Cm stop
295If the service is to be started as specified by
296.Xr rc.conf 5 ,
297stop the service.
298This should check that the service is running and complain if it is not.
299If
300.Cm forcestop
301is given, ignore the
302.Xr rc.conf 5
303check and attempt to stop.
304.It Cm restart
305Perform a
306.Cm stop
307then a
308.Cm start .
309.It Cm status
310If the script starts a process (rather than performing a one-off
311operation), show the status of the process.
312Otherwise it is not necessary to support this argument.
313Defaults to displaying the process ID of the program (if running).
314.It Cm poll
315If the script starts a process (rather than performing a one-off
316operation), wait for the command to exit.
317Otherwise it is not necessary to support this argument.
318.It Cm rcvar
319Display which
320.Xr rc.conf 5
321variables are used to control the startup of the service (if any).
322.El
323.Pp
324If a script must implement additional commands it can list them in
325the
326.Va extra_commands
327variable, and define their actions in a variable constructed from
328the command name (see the
329.Sx EXAMPLES
330section).
331.Pp
332The following key points apply to old-style scripts in
333.Pa /usr/local/etc/rc.d/ :
334.Pp
335.Bl -bullet
336.It
337Scripts are only executed if their
338.Xr basename 1
339matches the shell globbing pattern
340.Pa *.sh ,
341and they are executable.
342Any other files or directories present within the directory are silently
343ignored.
344.It
345When a script is executed at boot time, it is passed the string
346.Dq Li start
347as its first and only argument.
348At shutdown time, it is passed the string
349.Dq Li stop
350as its first and only argument.
351All
352.Nm rc.d/
353scripts are expected to handle these arguments appropriately.
354If no action needs to be taken at a given time
355(either boot time or shutdown time),
356the script should exit successfully and without producing an error message.
357.It
358The scripts within each directory are executed in lexicographical order.
359If a specific order is required,
360numbers may be used as a prefix to the existing filenames,
361so for example
362.Pa 100.foo
363would be executed before
364.Pa 200.bar ;
365without the numeric prefixes the opposite would be true.
366.It
367The output from each script is traditionally a space character,
368followed by the name of the software package being started or shut down,
369.Em without
370a trailing newline character (see the
371.Sx EXAMPLES
372section).
373.El
374.Sh SCRIPTS OF INTEREST
375When an automatic reboot is in progress,
376.Nm
377is invoked with the argument
378.Cm autoboot .
379One of the scripts run from
380.Pa /etc/rc.d/
381is
382.Pa /etc/rc.d/fsck .
383This script runs
384.Xr fsck 8
385with option
386.Fl p
387and
388.Fl F
389to
390.Dq preen
391all the disks of minor inconsistencies resulting
392from the last system shutdown.
393If this fails, then checks/repairs of serious inconsistencies
394caused by hardware or software failure will be performed
395in the background at the end of the booting process.
396If
397.Cm autoboot
398is not set, when going from single-user to multi-user mode for example,
399the script does not do anything.
400.Pp
401The
402.Nm rc.early
403script is run very early in the startup process, immediately before the
404file system check.
405The
406.Nm rc.early
407script is deprecated.
408Any commands in this
409file should be separated out into
410.Nm rc.d/
411style scripts and integrated into the
412.Nm
413system.
414.Pp
415The
416.Pa /etc/rc.d/local
417script can execute scripts from multiple
418.Nm rc.d/
419directories.
420The default locations are
421.Pa /usr/local/etc/rc.d/
422and
423.Pa /usr/X11R6/etc/rc.d/ ,
424but these may be overridden with the
425.Va local_startup
426.Xr rc.conf 5
427variable.
428.Pp
429The
430.Pa /etc/rc.d/serial
431script is used to set any special configurations for serial devices.
432.Pp
433The
434.Nm rc.firewall
435script is used to configure rules for the kernel based firewall
436service.
437It has several possible options:
438.Pp
439.Bl -tag -width ".Ar filename" -compact -offset indent
440.It Cm open
441will allow anyone in
442.It Cm client
443will try to protect just this machine
444.It Cm simple
445will try to protect a whole network
446.It Cm closed
447totally disables IP services except via
448.Pa lo0
449interface
450.It Cm UNKNOWN
451disables the loading of firewall rules
452.It Ar filename
453will load the rules in the given filename (full path required).
454.El
455.Pp
456The
457.Pa /etc/rc.d/atm*
458scripts are used to configure ATM network interfaces.
459The interfaces are configured in three passes.
460The first pass performs the initial interface configuration.
461The second pass completes the interface configuration and defines PVCs and
462permanent ATMARP entries.
463The third pass starts any ATM daemons.
464.Pp
465Most daemons, including network related daemons, have their own script in
466.Pa /etc/rc.d/ ,
467which can be used to start, stop, and check the status of the service.
468.Pp
469Any architecture specific scripts, such as
470.Pa /etc/rc.d/apm
471for example, specifically check that they are on that architecture
472before starting the daemon.
473.Pp
474Following tradition, all startup files reside in
475.Pa /etc .
476.Sh FILES
477.Bl -tag -compact
478.It Pa /etc/rc
479.It Pa /etc/rc.conf
480.It Pa /etc/rc.conf.local
481.It Pa /etc/rc.d/
482.It Pa /etc/rc.firewall
483.It Pa /etc/rc.local
484.It Pa /etc/rc.shutdown
485.It Pa /etc/rc.subr
486.It Pa /var/run/dmesg.boot
487.Xr dmesg 8
488results soon after the
489.Nm
490process begins.
491Useful when
492.Xr dmesg 8
493buffer in the kernel no longer has this information.
494.El
495.Sh EXAMPLES
496The following is a minimal
497.Nm rc.d/
498style script.
499Most scripts require little more than the following.
500.Bd -literal -offset indent
501#!/bin/sh
502#
503
504# PROVIDE: foo
505# REQUIRE: bar_service_required_to_precede_foo
506
507\&. /etc/rc.subr
508
509name="foo"
510rcvar=`set_rcvar`
511command="/usr/local/bin/foo"
512
513load_rc_config $name
514run_rc_command "$1"
515.Ed
516.Pp
517Certain scripts may want to provide enhanced functionality.
518The user may access this functionality through additional commands.
519The script may list and define as many commands at it needs.
520.Bd -literal -offset indent
521#!/bin/sh
522#
523
524# PROVIDE: foo
525# REQUIRE: bar_service_required_to_precede_foo
526# BEFORE:  baz_service_requiring_foo_to_precede_it
527
528\&. /etc/rc.subr
529
530name="foo"
531rcvar=`set_rcvar`
532command="/usr/local/bin/foo"
533extra_commands="nop hello"
534hello_cmd="echo Hello World."
535nop_cmd="do_nop"
536
537do_nop()
538{
539	echo "I do nothing."
540}
541
542load_rc_config $name
543run_rc_command "$1"
544.Ed
545.Pp
546As all processes are killed by
547.Xr init 8
548at shutdown, the explicit
549.Xr kill 1
550is unnecessary, but is often included.
551.Sh SEE ALSO
552.Xr kill 1 ,
553.Xr rc.conf 5 ,
554.Xr init 8 ,
555.Xr rcorder 8 ,
556.Xr rc.subr 8 ,
557.Xr reboot 8 ,
558.Xr savecore 8
559.Sh HISTORY
560The
561.Nm
562utility appeared in
563.Bx 4.0 .
564