FreeBSD 使用手冊
Prev | Chapter 11 設定與效能調校(Tuning) | Next

# 11.7 在 FreeBSD 使用 rc

Contributed by Tom Rhodes.

從 2002 年起，FreeBSD 整合了 NetBSD 的 `rc.d` 機制來作為系統服務啟動機制。 可以到 `/etc/rc.d` 目錄下去看，很多檔案都是基本服務，可以用 `start`, `stop` 及 `restart` 作為使用時的選項。 舉個例子，可以用下列指令來重新啟動 sshd(8)：

    # /etc/rc.d/sshd restart

其他服務也是類似作法。當然， 服務通常只要在 rc.conf(5) 內有指定的話，都會在開機時就自動啟動。舉例來說，若要開機時啟動 NAT(Network Address Translation) daemon 的話，只要在 `/etc/rc.conf` 內加上下列這行即可：

    natd_enable="YES"

若原本寫的是 `natd_enable="NO"` 那麼只要把 `NO` 改為 `YES` 就好了。rc scripts 會在下次重開機時，自動載入相關(有相依)的服務，以下我們會講到這部分。

Since the `rc.d` system is primarily intended to start/stop services at system startup/shutdown time, the standard `start`, `stop` and `restart` options will only perform their action if the appropriate `/etc/rc.conf` variables are set. For instance the above `sshd restart` command will only work if `sshd_enable` is set to `YES` in `/etc/rc.conf`. To `start`, `stop` or `restart` a service regardless of the settings in `/etc/rc.conf`, the commands should be prefixed with “force”. For instance to restart `sshd` regardless of the current `/etc/rc.conf` setting, execute the following command:

    # /etc/rc.d/sshd forcerestart

It is easy to check if a service is enabled in `/etc/rc.conf` by running the appropriate `rc.d` script with the option `rcvar`. Thus, an administrator can check that `sshd` is in fact enabled in `/etc/rc.conf` by running:

    # /etc/rc.d/sshd rcvar
    # sshd
    $sshd_enable=YES

> Note: The second line (`# sshd`) is the output from the `sshd` command, not a `root` console.

若要檢查服務是否有在運作，可以用 `status` 選項來查詢。比如：若要確認 `sshd` 是否真的有啟動的話，那麼打：

    # /etc/rc.d/sshd status
    sshd is running as pid 433.

In some cases it is also possible to `reload` a service. This will attempt to send a signal to an individual service, forcing the service to reload its configuration files. In most cases this means sending the service a `SIGHUP` signal. Support for this feature is not included for every service.

The `rc.d` system is not only used for network services, it also contributes to most of the system initialization. For instance, consider the `bgfsck` file. When this script is executed, it will print out the following message:

    Starting background file system checks in 60 seconds.

Therefore this file is used for background file system checks, which are done only during system initialization.

Many system services depend on other services to function properly. For example, NIS and other RPC-based services may fail to start until after the `rpcbind` (portmapper) service has started. To resolve this issue, information about dependencies and other meta-data is included in the comments at the top of each startup script. The rcorder(8) program is then used to parse these comments during system initialization to determine the order in which system services should be invoked to satisfy the dependencies. The following words may be included at the top of each startup file:

- `PROVIDE`: Specifies the services this file provides.

- `REQUIRE`: Lists services which are required for this service. This file will run after the specified services.

- `BEFORE`: Lists services which depend on this service. This file will run before the specified services.

By using this method, an administrator can easily control system services without the hassle of “runlevels” like some other UNIX® operating systems.

Additional information about the `rc.d` system can be found in the rc(8) and rc.subr(8) manual pages.

Prev | Home | Next
設定 `cron` | Up | 設定網路卡

本文及其他文件，可由此下載：ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/。

若有 FreeBSD 方面疑問，請先閱讀 FreeBSD 相關文件，如不能解決的話，再洽詢 <questions@FreeBSD.org>。
關於本文件的問題，請洽詢 <doc@FreeBSD.org>。