sudo service status 包含错误;

$ sudo service cassandra status
● cassandra.service - LSB: distributed storage system for structured data
   Loaded: loaded (/etc/init.d/cassandra; bad; vendor preset: enabled)
   Active: active (running) since Wed 2016-10-12 15:54:40 IDT; 4min 4s ago

第二行输出中的bad;部分代表什么意思? 我在许多服务中都遇到过这个问题,例如mysql、winbind、virtualbox,其中一些我已经完美地使用过(cassandra是最新安装的)。
1个回答

简短回答:
  • bad:它显示了Systemd Unit files的启用状态
  • 在使用systemd的系统上,您会看到这种类型的消息
  • 您可以使用以下命令检查启用状态:

    sudo systemctl is-enabled <unit-name>
    

    如果该单元文件是本机的systemd服务,则会输出enableddisabled等。如果不是本机的systemd服务,则会报告如下消息。

    sudo systemctl is-enabled apache2
    apache2.service不是本机服务,正在重定向到systemd-sysv-install
    执行/lib/systemd/systemd-sysv-install is-enabled apache2
    enabled
    

    但是使用以下命令:

    systemctl status apache2
    或者
    service apache2 status
    

    它会给出bad的状态。(可能是因为无法打印完整的消息或开发人员决定打印bad

长答案:

系统单元文件是什么?

单元是systemd知道如何管理的对象。它们基本上是一种标准化的系统资源表示,可以由套件中的守护进程进行管理并通过提供的实用工具进行操作。它可以用于抽象服务、网络资源、设备、文件系统挂载和隔离的资源池。 您可以在这里详细了解systemd单元herehere

示例:

systemctl status apache2
* apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           `-apache2-systemd.conf
   Active: active (running) since Wed 2016-10-12 14:29:42 UTC; 17s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1027 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)

systemctl会检查apache2是否是本地单元。如果不是,它将要求systemd-sysv-generator生成一个以单元格式提供类似本地单元支持的文件。在上面的示例中,生成的文件保存在/lib/systemd/system/apache2.service.d/apache2-systemd.conf中。
Drop-In: /lib/systemd/system/apache2.service.d
               `-apache2-systemd.conf

注意:您可以在“/lib/systemd/system-generators/systemd-sysv-generator”找到该生成器,并且您可以阅读更多相关内容。
man systemd-sysv-generator

主要观点:

is-enabled NAME...
       Checks whether any of the specified unit files are enabled (as with
       enable). Returns an exit code of 0 if at least one is enabled,
       non-zero otherwise. Prints the current enable status (see table).
       To suppress this output, use --quiet.

       Table 1.  is-enabled output
       +------------------+-------------------------+-----------+
       |Name              | Description             | Exit Code |
       +------------------+-------------------------+-----------+
       |"enabled"         | Enabled via             |           |
       +------------------+ .wants/, .requires/     |           |
       |"enabled-runtime" | or alias symlinks       |           |
       |                  | (permanently in         | 0         |
       |                  | /etc/systemd/system/,   |           |
       |                  | or transiently in       |           |
       |                  | /run/systemd/system/).  |           |
       +------------------+-------------------------+-----------+
       |"linked"          | Made available through  |           |
       +------------------+ one or more symlinks    |           |
       |"linked-runtime"  | to the unit file        |           |
       |                  | (permanently in         |           |
       |                  | /etc/systemd/system/    |           |
       |                  | or transiently in       | > 0       |
       |                  | /run/systemd/system/),  |           |
       |                  | even though the unit    |           |
       |                  | file might reside       |           |
       |                  | outside of the unit     |           |
       |                  | file search path.       |           |
       +------------------+-------------------------+-----------+
       |"masked"          | Completely disabled,    |           |
       +------------------+ so that any start       |           |
       |"masked-runtime"  | operation on it fails   |           |
       |                  | (permanently in         | > 0       |
       |                  | /etc/systemd/system/    |           |
       |                  | or transiently in       |           |
       |                  | /run/systemd/systemd/). |           |
       +------------------+-------------------------+-----------+
       |"static"          | The unit file is not    | 0         |
       |                  | enabled, and has no     |           |
       |                  | provisions for enabling |           |
       |                  | in the "[Install]"      |           |
       |                  | section.                |           |
       +------------------+-------------------------+-----------+
       |"indirect"        | The unit file itself is | 0         |
       |                  | not enabled, but it has |           |
       |                  | a non-empty Also=       |           |
       |                  | setting in the          |           |
       |                  | "[Install]" section,    |           |
       |                  | listing other unit      |           |
       |                  | files that might be     |           |
       |                  | enabled.                |           |
       +------------------+-------------------------+-----------+
       |"disabled"        | Unit file is not        | > 0       |
       |                  | enabled, but contains   |           |
       |                  | an "[Install]" section  |           |
       |                  | with installation       |           |
       |                  | instructions.           |           |
       +------------------+-------------------------+-----------+
       |"bad"             | Unit file is invalid or | > 0       |
       |                  | another error occurred. |           |
       |                  | Note that is-enabled    |           |
       |                  | will not actually       |           |
       |                  | return this state, but  |           |
       |                  | print an error message  |           |
       |                  | instead. However the    |           |
       |                  | unit file listing       |           |
       |                  | printed by              |           |
       |                  | list-unit-files might   |           |
       |                  | show it.                |           |
       +------------------+-------------------------+-----------+

如果我们运行以下命令:
sudo systemctl is-enabled ssh
enabled

sudo systemctl is-enabled docker
enabled

sudo systemctl is-enabled apache2
apache2.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install is-enabled apache2
enabled

你可以看到如果单位是systemd的本地单位,比如sshdocker,在上面的输出中只会显示enabled。对于那些不是本地单位但仍然启用的单位,它会给出相应的消息,而不是打印bad,因为有这个条件:
       +------------------+-------------------------+-----------+
       |"bad"             | Unit file is invalid or | > 0       |
       |                  | another error occurred. |           |
       |                  | Note that is-enabled    |           |
       |                  | will not actually       |           |
       |                  | return this state, but  |           |
       |                  | print an error message  |           |
       |                  | instead. However the    |           |
       |                  | unit file listing       |           |
       |                  | printed by              |           |
       |                  | list-unit-files might   |           |
       |                  | show it.                |           |
       +------------------+-------------------------+-----------+
解决方案:

状态bad不会造成问题(我不确定是否有关系),但它无法提供systemctl的所有功能。 您可以等待下一个版本的package,该版本将原生支持systemd。或者,您可以使用给定的参考文献编写服务或任何其他资源的单元文件。

您可以通过以下参考文献详细了解systemd、systemctl和units:

  1. Systemctl

  2. Systemd unitsHere

  3. Systemd


非常感谢对这个话题的全面和周到的考虑! - matanster
2这个回答看起来非常完整,但有点令人困惑...你似乎提供了很多信息,却没有明确陈述出人们可能(也许)从这些信息中得出的简洁结论。例如,我从你的简短回答中推断出(需要一些工作),当一个服务(systemd单元)不是本地的时候,systemctl无法获取其启用状态,除非重定向到systemd-sysv-install。systemctl status命令由于某种原因没有这样做,而是报告“bad”,而systemctl is-enabled会进行重定向以给出状态。 - EricS
1我觉得让我感到困惑的是一些措辞,尤其是在开头的要点中。我提交了一些编辑意见,试图改进。 - EricS