【问题标题】:How to get update-rc.d or insserv to follow dependencies如何获取 update-rc.d 或 insserv 以遵循依赖关系
【发布时间】:2014-01-01 21:30:15
【问题描述】:

我一直在尝试设置一个脚本以在 Debian 7.1 系统上启动时运行一段时间,但没有成功。我试过同时使用 insserv 和 update-rc.d,但我的问题似乎与这两种工具相同。这是我的脚本的 LSB 部分:

#!/bin/bash

### BEGIN INIT INFO
# Provides:             start_guest
# Required-Start:       $bootlogs $sudo $virtualbox-guest-utils $syslog
# Required-Stop:        $bootlogs $sudo $virtualbox-guest-utils $syslog
# Should-Start:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    start_guest
### END INIT INFO

使用 update-rc.d,以下是我尝试过的各种命令,结果都一样:

sudo update-rc.d start_guest defaults
sudo update-rc.d start_guest defaults 22
sudo update-rc.d start_guest start 22 2 3 4 5 . stop 78 2 3 4 5 .

无论我运行哪一个,都会被告知(仅显示运行级别 2,因为 2、3、4、5 相同,0、1、6 都是 K01):

insserv: remove service /etc/init.d/../rc2.d/S21rc.local
insserv: enable service ../init.d/rc.local -> /etc/init.d/../rc2.d/S20rc.local
insserv: remove service /etc/init.d/../rc2.d/S21rmnologin
insserv: enable service ../init.d/rmnologin -> /etc/init.d/../rc2.d/S20rmnologin
insserv: enable service ../init.d/start_guest -> /etc/init.d/../rc2.d/S17start_guest

无论我得到什么样的依赖信息,我都无法让它从 S17 以外的地方开始。不幸的是,../rc2.d/S19bootlogs 将在我的脚本之后启动,这将阻止我有时获取关键的日志信息。

当我尝试用 insserv 做同样的事情时,我被告知正常服务已被指示启动我的新服务,但它似乎仍然没有遵循依赖顺序。在 start_guest 开始运行之前,我需要运行更多服务(例如 sudo、virtualbox-guest-utils 等)

me@bronze:/etc/init.d# sudo insserv start_guest 
insserv: Service remote_fs has to be enabled to start service start_guest
insserv: Service syslog has to be enabled to start service start_guest
insserv: exiting now!

David Krmpotic's answer to this question 几乎回答了我的问题,但似乎也没有遵循“必需启动”依赖项。

如何让我的脚本在启动时运行遵循给定的依赖项?谢谢!

【问题讨论】:

    标签: debian init


    【解决方案1】:

    事实证明,可以通过三种不同的方式将依赖项添加到 LSB 标头中,并且很少有文档可以解释这些差异。对我而言,正确的 LSB 标头实际上是这样的:

    # Required-Start:       bootlogs sudo virtualbox-guest-utils $syslog
    # Required-Stop:        bootlogs sudo virtualbox-guest-utils $syslog
    

    (在这两行中,我都可以删除 sudo 和 virtualbox-guest-utils,因为引导日志会强制我的 start_guest 脚本启动最新的。)

    一旦我的 LSB 标头遵循此语法,我就能够运行 update-rc.d start_guest defaults,它完全符合我的要求。

    当列出需要在新的初始化脚本之前或之后启动或停止的服务时,有几种方法可以列出它们。

    • 使用$name 指代facility service 或虚拟服务
    • 使用name 来引用单个现有服务
    • 使用+name 指代可选服务

    其中一些在文档中提到,但在各个部分甚至各种文档之间并不清楚和分解。我遇到的每个例子都只使用了设施服务,没有包括任何正常或可选的服务,导致我的语法非常混乱。

    来自 insserv 的手册页:

    insserv  scans  for  System Facilities in the configuration file /etc/insserv.conf
    and each file in the directory /etc/insserv.conf.d/.  Each line which begins with
    $ and a following name defines a system facility accordingly to the Linux Standard
    Base Specification (LSB)
    

    和:

    Names starting  with a `+' sign are marked as optional.  If the service with the
    name after the plus sign is available it will be used,  if  not  available  it
    is  ignored silently.
    

    我不打算先问然后回答我自己的问题。我已经为这个问题困惑了一个多月,虽然我得到了提示,但没有给我一个足够完整的答案,我可以看到它在做我需要它做的事情。几周后重新审视事情让我有了更好的洞察力,即使仍然涉及到令人费解的问题。

    【讨论】:

    • 感谢您的提问和回答,但我仍然无法添加对 sudo 的依赖。 sudo 的优先级为 75,而我依赖于 sudo 的脚本的优先级为 20。
    • 据我所见,优先级为 75 的 sudo 相当低。我的大多数服务的优先级为 25 或更低,即使是我添加的那些。当您运行像 sudo update-rc.d sudo defaults 18 这样的行时会发生什么(其中 18 是 sudo 应该在您的系统上启动的合理数字)?
    • 但是,那并不能证明什么。如果 sudo 优先级更改为 18,那么我的脚本有 20 将是​​可预测的结果。依赖解决的真正证明将是是否尊重原始优先级。
    【解决方案2】:

    扩展MtWoRw's answer...

    将依赖项添加到 LSB 标头时,请确保依赖项与依赖项的 init 脚本的“Provides:”字段中的标识符匹配,而不是与 / 中依赖项脚本的名称匹配etc/init.d.

    我试图让 mpdavahi-daemon 之后启动时遇到了这个问题。在 Raspbian 中,avahi-daemon 初始化脚本“提供:avahi”。 insserv 抱怨必须在 mpd 之前启用 avahi-daemon 失败,直到我将 LSB 标头更改为使用 avahi, 而不是 avahi-daemon。

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      相关资源
      最近更新 更多