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