关于“Condition...”失败的“systemctl status”信息消失了,这有什么用?

关于“Condition...”失败的“systemctl status”信息消失了,这有什么用?

我创建了一个条件失败的测试单元,并启动了它。 systemctl status可以显示有关条件失败的信息...但是仅有的如果我设法让我的测试服务处于失败状态。

# systemctl status test.service -n0
● test.service
   Loaded: loaded (/etc/systemd/system/test.service; static; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2018-07-10 17:01:35 BST; 16s ago
Condition: start condition failed at Tue 2018-07-10 17:01:51 BST; 1s ago
           └─ ConditionKernelCommandLine=not-an-option was not met
 Main PID: 4378 (code=exited, status=1/FAILURE)

失败状态是阻止单元被垃圾收集和遗忘的一种方法。但否则,单位会被遗忘,您将无法看到条件信息。条件失败也不会记录到日志中 - 仅记录我之前设计的失败状态。

# systemctl reset-failed test.service
# systemctl status test.service
● test.service
   Loaded: loaded (/etc/systemd/system/test.service; static; vendor preset: disabled)
   Active: inactive (dead)

Jul 10 17:01:35 fedora28-vm systemd[1]: Starting test.service...
Jul 10 17:01:35 fedora28-vm systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
Jul 10 17:01:35 fedora28-vm systemd[1]: test.service: Failed with result 'exit-code'.
Jul 10 17:01:35 fedora28-vm systemd[1]: Failed to start test.service.

那么你应该如何使用这个systemctl功能呢?通常,当您启动设备时,将首先测试失败的条件,然后跳过启动过程。所以它没有机会进入失败状态。

在 systemd 版本 v239-120-g0fad72fae 上进行测试。

答案1

一旦您允许您的设备作为引导过程的一部分启动,它就可以正常工作。如果单元被诸如 之类的活动单元所依赖,则单元将被阻止被垃圾收集multi-user.target

我将这些行添加到单元中

[Install]
WantedBy=multi-user.target

然后我就可以按照我想要的方式使用它:

# systemctl enable test.service
# systemctl start test.service
# systemctl status test.service -n0

● test.service
   Loaded: loaded (/etc/systemd/system/test.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
Condition: start condition failed at Tue 2018-07-10 17:18:21 BST; 3s ago
           └─ ConditionKernelCommandLine=not-an-option was not met

相关内容