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