FreeBSD 中文社区 2025 第二季度问卷调查
FreeBSD 中文社区(CFC)
VitePress 镜像站QQ 群 787969044视频教程Ⅰ视频教程Ⅱ
  • FreeBSD 从入门到追忆
  • 中文期刊
  • 状态报告
  • 发行说明
  • 手册
  • 网络文章集锦
  • 笔记本支持报告
  • Port 开发者手册
  • 架构手册
  • 开发者手册
  • 中文 man 手册
  • 文章
  • 书籍
  • man
  • man1
    • Mail.1
    • addr2line.1
    • alias.1
    • alloc.1
    • apply.1
    • apropos.1
    • ar.1
    • at.1
    • atq.1
    • atrm.1
    • awk.1
    • basename.1
    • batch.1
    • bc.1
    • bg.1
    • bind.1
    • bindkey.1
    • break.1
    • breaksw.1
    • bsdcat.1
    • bsdtar.1
    • btsockstat.1
    • builtin.1
    • builtins.1
    • c++.1
    • case.1
    • cat.1
    • cc.1
    • chdir.1
    • chfn.1
    • chgrp.1
    • chkey.1
    • chmod.1
    • cksum.1
    • clang++.1
    • clang-cpp.1
    • cmp.1
    • colrm.1
    • column.1
    • comm.1
    • command.1
    • complete.1
    • continue.1
    • cp.1
    • cpp.1
    • crypt.1
    • csh.1
    • ctfdump.1
    • cut.1
    • date.1
    • dd.1
    • default.1
    • df.1
    • dialog.1
    • diff.1
    • diff3.1
    • dirname.1
    • dirs.1
    • do.1
    • done.1
    • dtrace.1
    • du.1
    • echo.1
    • echotc.1
    • edit.1
    • ee.1
    • egrep.1
    • elfdump.1
    • elif.1
    • else.1
    • end.1
    • endif.1
    • endsw.1
    • enigma.1
    • env.1
    • esac.1
    • eval.1
    • exec.1
    • exit.1
    • export.1
    • fc.1
    • fg.1
    • fgrep.1
    • fi.1
    • filetest.1
    • find.1
    • for.1
    • foreach.1
    • fsync.1
    • ftp.1
    • fuser.1
    • gate-ftp.1
    • getopts.1
    • glob.1
    • goto.1
    • grep.1
    • groups.1
    • gunzip.1
    • gzcat.1
    • gzip.1
    • hash.1
    • hashstat.1
    • head.1
    • history.1
    • host.1
    • hostname.1
    • hup.1
    • ibv_asyncwatch.1
    • ibv_devices.1
    • ibv_devinfo.1
    • ibv_ud_pingpong.1
    • id.1
    • ident.1
    • if.1
    • intro.1
    • introduction.1
    • jobid.1
    • jobs.1
    • kdump.1
    • kenv.1
    • keylogin.1
    • keylogout.1
    • kill.1
    • killall.1
    • ktrace.1
    • last.1
    • lastcomm.1
    • ld.1
    • ld.lld.1
    • ldd.1
    • ldd32.1
    • less.1
    • lessecho.1
    • limit.1
    • limits.1
    • link.1
    • llvm-addr2line.1
    • llvm-ranlib.1
    • ln.1
    • locate.1
    • lockstat.1
    • log.1
    • logger.1
    • login.1
    • logname.1
    • logout.1
    • look.1
    • lorder.1
    • ls-F.1
    • ls.1
    • lsvfs.1
    • lzcat.1
    • lzma.1
    • lzmadec.1
    • lzmainfo.1
    • mailx.1
    • make.1
    • man.1
    • mandoc.1
    • manpath.1
    • md5.1
    • mdocml.1
    • mkdir.1
    • mkfilters.1
    • more.1
    • mv.1
    • nawk.1
    • nc.1
    • netstat.1
    • newaliases.1
    • nice.1
    • notify.1
    • onintr.1
    • pagesize.1
    • passwd.1
    • paste.1
    • pax.1
    • perror.1
    • pftp.1
    • popd.1
    • printenv.1
    • ps.1
    • pushd.1
    • pwd.1
    • qemu.1
    • ranlib.1
    • read.1
    • readelf.1
    • readlink.1
    • readonly.1
    • realpath.1
    • ree.1
    • rehash.1
    • repeat.1
    • rev.1
    • revoke.1
    • rgrep.1
    • rm.1
    • rmd16.1
    • rmdir.1
    • rping.1
    • ruptime.1
    • rwho.1
    • sched.1
    • scp.1
    • sdiff.1
    • sed.1
    • seq.1
    • set.1
    • setenv.1
    • settc.1
    • setty.1
    • setvar.1
    • sftp.1
    • sh.1
    • sha1.1
    • sha224.1
    • sha256.1
    • sha384.1
    • sha512.1
    • sha512t256.1
    • shift.1
    • skein1024.1
    • skein256.1
    • skein512.1
    • slogin.1
    • sockstat.1
    • sort.1
    • source.1
    • split.1
    • ssh-copy-id.1
    • ssh-keygen.1
    • ssh.1
    • stat.1
    • stop.1
    • su.1
    • suspend.1
    • svnlite.1
    • switch.1
    • systat.1
    • tail.1
    • tar.1
    • tcpdump.1
    • tcsh.1
    • telltc.1
    • telnet.1
    • then.1
    • time.1
    • timeout.1
    • times.1
    • top.1
    • touch.1
    • trap.1
    • truncate.1
    • truss.1
    • type.1
    • ulimit.1
    • umask.1
    • unalias.1
    • uname.1
    • uncomplete.1
    • unhash.1
    • uniq.1
    • unlimit.1
    • unlink.1
    • unlzma.1
    • unset.1
    • unsetenv.1
    • until.1
    • unxz.1
    • uptime.1
    • users.1
    • uuidgen.1
    • view.1
    • w.1
    • wait.1
    • wall.1
    • wc.1
    • whatis.1
    • where.1
    • whereis.1
    • which.1
    • while.1
    • who.1
    • whoami.1
    • xargs.1
    • xz.1
    • xzcat.1
    • xzdec.1
    • xzdiff.1
    • yes.1
    • yppasswd.1
    • zcat.1
    • zcmp.1
    • zdiff.1
    • zforce.1
    • zless.1
    • zmore.1
    • znew.1
  • man4
    • ow.4
    • ow_temp.4
    • owc.4
  • man5
    • device.hints.5
  • man7
    • hire
  • man8
    • adduser.8
    • authpf-noip.8
    • authpf.8
    • autounmountd.8
    • beastie.4th.8
    • bhyve.8
    • bhyvectl.8
    • bhyveload.8
    • blacklistctl.8
    • boot.8
    • boot0cfg.8
    • boot_i386.8
    • bsdconfig.8
    • camcontrol.8
    • check-password.4th.8
    • chown.8
    • chroot.8
    • config.8
    • crash.8
    • crashinfo.8
    • devctl.8
    • devinfo.8
    • devmatch.8
    • diskinfo.8
    • dmesg.8
    • fastboot.8
    • fasthalt.8
    • freebsd-update.8
    • fstyp.8
    • ftp-proxy.8
    • ftpd.8
    • gpart.8
    • gpioctl.8
    • gptboot.8
    • gptzfsboot.8
    • growfs.8
    • halt.8
    • i2c.8
    • ifconfig.8
    • inetd.8
    • init.8
    • intro.8
    • iostat.8
    • ipfw.8
    • jail.8
    • jexec.8
    • jls.8
    • kldconfig.8
    • kldload.8
    • kldstat.8
    • kldunload.8
    • kldxref.8
    • lastlogin.8
    • loader.4th.8
    • loader.8
    • loader.efi.8
    • mail.local.8
    • mailstats.8
    • menu.4th.8
    • mount.8
    • mount_cd9660.8
    • mount_msdosfs.8
    • moused.8
    • mtree.8
    • natd.8
    • nmtree.8
    • ntptime.8
    • password.lua.8
    • periodic.8
    • ping.8
    • pkg.8
    • pmccontrol.8
    • pmcstat.8
    • powerd.8
    • poweroff.8
    • pw.8
    • pwm.8
    • quot.8
    • rarpd.8
    • rc.8
    • rc.d.8
    • rc.firewall.8
    • rc.local.8
    • rc.network.8
    • rc.pccard.8
    • rc.resume.8
    • rc.serial.8
    • rc.shutdown.8
    • rc.subr.8
    • rcorder.8
    • reboot.8
    • renice.8
    • rescue.8
    • rmuser.8
    • route.8
    • route6d.8
    • routed.8
    • rpc.rusersd.8
    • service.8
    • shutdown.8
    • spi.8
    • ssh-keysign.8
    • sysctl.8
    • sysrc.8
    • tcpdrop.8
    • tftpd.8
    • tzsetup.8
    • umount.8
    • vmstat.8
    • watchdog.8
    • watchdogd.8
    • zfs.8
由 GitBook 提供支持
LogoLogo

FreeBSD 中文社区(CFC) 2025

在本页
  • 名称
  • 概要
  • 描述
  • 操作
  • rc.shutdown 操作
  • rc.d/ 内容
  • 感兴趣的脚本
  • 文件
  • 实例
  • 参见
  • 历史
在GitHub上编辑
导出为 PDF
  1. man8

rc.shutdown.8

上一页rc.serial.8下一页rc.subr.8

最后更新于2个月前

RC(8)

RC(8)

FreeBSD System Manager's Manual

RC(8)

rc —

自动重启和守护进程启动的命令脚本

rc rc.conf rc.conf.local rc.d/ rc.firewall rc.local rc.resume rc.shutdown rc.subr

rc 实用程序是在被 init(8) 调用后控制自动引导过程的命令脚本。 rc.local 脚本包含仅与特定站点相关的命令。 通常,现在使用 /usr/local/etc/rc.d/ 机制代替 rc.local ,但如果您想使用 rc.local ,它仍然受支持。 在这种情况下,它应该来自 /etc/rc.conf 并包含您系统的额外自定义启动代码。 然而,处理 rc.local 的最佳方法是将其分离为 rc.d/ 样式的脚本,并将它们放在 /usr/local/etc/rc.d/ 下。 rc.conf 文件包含启动脚本引用的全局系统配置信息,而 rc.conf.local 包含本地系统配置。有关详细信息,请参阅 rc.conf(5) 。

rc.d/ 目录包含将在启动时和关机时自动执行的脚本。

service(8) 命令提供了一个方便的接口来管理 rc.d 服务。

sysrc(8) 命令提供了一个脚本接口来修改系统配置文件。

操作

  1. 如果是自动引导,设置 autoboot=yes 并启用一个标志 (rc_fast=yes), 它可以防止 rc.d/ 脚本对已经运行的进程进行检查(从而加快引导过程)。 在退出单用户 shell 后启动 rc 时,不会发生此 rc_fast=yes 加速。

  2. 确定系统是否在无盘引导,如果是,则运行 /etc/rc.initdiskless 脚本。

  3. 来源 /etc/rc.subr 以加载各种 rc.subr(8) 函数以使用。

  4. 加载配置文件。

  5. 确定是否在 jail 中启动,并将 “nojail” (不允许 jail )或 “nojailvnet” (仅允许启用 vnet 的 jail )添加到要在 rcorder(8) 中跳过的关键字列表中。

  6. 如果文件 ${firstboot_sentinel} 不存在,请在 rcorder(8) 中将 “firstboot” 添加到要跳过的关键字列表中。

  7. 调用 rcorder(8) 对 /etc/rc.d/ 中没有 “nostart” 关键字的文件进行排序(参考 rcorder(8) 的 -s 标志)。

  8. 使用 run_rc_script() (来自 rc.subr(8)) 依次调用每个脚本,将 $1 设置为 “start”, 并在子 shell 中获取脚本。 当作为 $early_late_divider 值的脚本运行时停止处理。

  9. 再次检查文件 ${firstboot_sentinel} 是否存在(如果它位于新安装的文件系统上)并调整关键字列表以适当跳过。

  10. 重新运行 rcorder(8), 这次包括 $local_startup 目录中的脚本。 忽略 $early_late_divider 之前的所有内容,然后开始执行上述脚本。

  11. 如果文件 ${firstboot_sentinel} 存在,请将其删除。 如果文件 ${firstboot_sentinel}-reboot 也存在(因为它是由脚本创建的),则将其删除并重新启动。

rc.shutdown 操作

  1. 将 rc_shutdown 设置为传递给 rc.shutdown 的第一个参数的值,如果没有传递任何参数,则设置为 “unspecified” 。

  2. 来源 /etc/rc.subr 以加载各种 rc.subr(8) shell 函数以使用。

  3. 加载配置文件。

  4. 调用 rcorder(8) 对 /etc/rc.d/ 和 $local_startup 目录中具有 “shutdown” 关键字的文件进行排序(参考 rcorder(8) 的 -k 标志),颠倒该顺序,并分配结果为一个变量。

  5. 使用 run_rc_script() (来自 rc.subr(8)) 依次调用每个脚本,将 $1 设置为 “faststop” ,并在子 shell 中获取脚本。

rc.d/ 内容

rc.d/ 位于 /etc/rc.d/ 。当前在 rc.d/ 中使用以下文件命名约定:

ALLUPPERCASE

作为 “placeholders” 的脚本,以确保某些操作在其他操作之前执行。 按照启动顺序,这些是:

FILESYSTEMS

确保安装了根文件系统和其他关键文件系统。 这是默认的 $early_late_divider 。

NETWORKING

确保基本网络服务正在运行,包括一般网络配置。

SERVERS

确保早期启动的服务(例如 nisdomain )存在基本服务,因为下面的 DAEMON 需要这些服务。

DAEMON

在所有通用守护进程(如 lpd 和 ntpd )之前检查点。

LOGIN

用户登录服务 (inetd 和 sshd) 之前的检查点,以及可能以用户身份运行命令的服务 (cron 和 sendmail) 。

bar

源自子外壳的脚本。 如果这样的脚本以非零状态终止,则引导不会停止,但如果需要,脚本可以通过调用 stop_boot() 函数(来自 rc.subr(8)) 来停止引导。

每个脚本都应包含 rcorder(8) 关键字,尤其是适当的 “PROVIDE” 条目,以及必要时的 “REQUIRE” 和 “BEFORE” 关键字。

每个脚本都应至少支持以下参数,如果它使用 run_rc_command() 函数,则会自动支持这些参数:

启动服务。 这应该检查服务是否按照 rc.conf(5)- 的规定启动。 还检查服务是否已经在运行,如果是则拒绝启动。 如果系统直接启动到多用户模式,则标准 FreeBSD 脚本不会执行后一种检查,以加快启动过程。 如果给出了 forcestart ,忽略 rc.conf(5) 检查并继续启动。

如果要按照 rc.conf(5) 的规定启动服务,请停止服务。 这应该检查服务是否正在运行,如果没有运行则抱怨。 如果给出 forcestop ,忽略 rc.conf(5) 检查并尝试停止。

执行 stop 然后 start 。

如果脚本启动一个进程(而不是执行一次性操作),则显示该进程的状态。 否则没有必要支持这个论点。 默认显示程序的进程 ID(如果正在运行)。

在 rc.conf(5) 中启用该服务。

在 rc.conf(5) 中禁用该服务。

从 rc.conf(5) 中删除该服务。如果 ‘`service_delete_empty`’ 设置为 “YES” ,修改后 /etc/rc.conf.d/$servicename 为空会被删除。

打印脚本功能的简短描述。

打印脚本的非标准命令。

如果脚本启动一个进程(而不是执行一次性操作),请等待命令退出。 否则没有必要支持这个论点。

如果服务启用则返回 0,否则返回 1。 此命令不打印任何内容。

显示哪些 rc.conf(5) 变量用于控制服务的启动(如果有)。

以下关键点适用于 /usr/local/etc/rc.d/ 中的旧式脚本:

  • 只有当它们的 basename(1) 与 shell globbing 模式 *.sh 匹配并且它们是可执行的时,才会执行脚本。 目录中存在的任何其他文件或目录都将被静默忽略。

  • 当一个脚本在启动时执行时,它会传递字符串 “start” 作为它的第一个也是唯一的参数。 在关机时,它被传递字符串 “stop” 作为它的第一个也是唯一的参数。 所有 rc.d/ 脚本都应该适当地处理这些参数。 如果在给定时间(启动时间或关闭时间)不需要采取任何操作,则脚本应该成功退出并且不会产生错误消息。

  • 每个目录中的脚本按字典顺序执行。 如果需要特定的顺序,可以使用数字作为现有文件名的前缀,例如 100.foo 将在 200.bar; 之前执行;如果没有数字前缀,则相反。

  • 每个脚本的输出通常是一个空格字符,后跟正在启动或关闭的软件包的名称,后面 没有 换行符。

当自动重启正在进行时,使用参数 autoboot 调用 rc 。 从 /etc/rc.d/ 运行的脚本之一是 /etc/rc.d/fsck 。 此脚本使用选项 -p 和 -F 运行 fsck(8) ,以 “preen” 上次系统关闭导致的所有轻微不一致的磁盘。 如果此操作失败,则将在引导过程结束时在后台执行由硬件或软件故障引起的严重不一致的检查/修复。 如果未设置 autoboot ,例如从单用户模式转到多用户模式时,脚本不会执行任何操作。

/etc/rc.d/local 脚本可以从多个 rc.d/ 目录执行脚本。 默认位置包括 /usr/local/etc/rc.d/ ,但这些可能会被 local_startup rc.conf(5) 变量覆盖。

/etc/rc.d/serial 脚本用于设置串行设备的任何特殊配置。

rc.firewall 脚本用于为基于内核的防火墙服务配置规则。它有几个可能的选项:

将允许任何人进入

将尝试仅保护这台机器

将尝试保护整个网络

完全禁用 IP 服务,除了通过 lo0 接口

禁用加载防火墙规则

filename

将加载给定文件名中的规则(需要完整路径)。

大多数守护进程,包括网络相关的守护进程,在 /etc/rc.d/ 中都有自己的脚本,可用于启动、停止和检查服务的状态。

任何体系结构特定的脚本,例如 /etc/rc.d/apm ,在启动守护程序之前专门检查它们是否在该体系结构上。

按照传统,所有启动文件都驻留在 /etc 中。

/etc/rc

/etc/rc.conf

/etc/rc.conf.local

/etc/rc.d/

/etc/rc.firewall

/etc/rc.local

/etc/rc.shutdown

/etc/rc.subr

/var/run/dmesg.boot

dmesg(8) 会在 rc 进程开始后立即生成。 当内核中的 dmesg(8) 缓冲区不再具有此信息时很有用。

以下是一个最小的 rc.d/ 样式脚本。 大多数脚本只需要以下内容。

#!/bin/sh # # PROVIDE: foo # REQUIRE: bar_service_required_to_precede_foo . /etc/rc.subr name="foo" rcvar=foo_enable command="/usr/local/bin/foo" load_rc_config $name run_rc_command "$1"

某些脚本可能希望提供增强的功能。 用户可以通过附加命令访问此功能。该脚本可以根据需要列出和定义任意数量的命令。

#!/bin/sh # # PROVIDE: foo # REQUIRE: bar_service_required_to_precede_foo # BEFORE: baz_service_requiring_foo_to_precede_it . /etc/rc.subr name="foo" rcvar=foo_enable command="/usr/local/bin/foo" extra_commands="nop hello" hello_cmd="echo Hello World." nop_cmd="do_nop" do_nop() { echo "I do nothing." } load_rc_config $name run_rc_command "$1"

由于所有进程在关闭时都会被 init(8) 杀死,因此显式 kill(1) 是不必要的,但通常会包含在内。

kill(1), rc.conf(5), init(8), rc.resume(8), rc.subr(8), rcorder(8), reboot(8), savecore(8), service(8), sysrc(8)

rc 实用程序出现在 4.0BSD 中。

October 6, 2020

FreeBSD 13.1-RELEASE

如果脚本必须实现额外的命令,它可以在 extra_commands 变量中列出它们,并在由命令名称构造的变量中定义它们的操作(参见 部分)。

名称
概要
描述
start
stop
restart
status
enable
disable
delete
describe
extracommands
poll
enabled
rcvar
示例
感兴趣的脚本
open
client
simple
closed
UNKNOWN
文件
实例
参见
历史