当启动/停止失败时,systemctl应该显示输出吗?

时间:2018-09-07 18:49:55

标签: service systemd systemctl

我已经用谷歌搜索了这个问题的所有变体,但我只是想知道有关失败服务的问题,而不是有关systemctl如何对待它们的问题。我有一个已经作为init.d脚本运行的服务。我们现在正在使用systemctl,很好。我创建了一个服务文件,该文件是由systemd-sysv-generator自动生成的文件的轻微修改版本。对于ExecStart和ExecStop,它会调用bash脚本,如果启动/停止成功,则该脚本将返回0,否则将返回非零。

我了解“ systemctl启动/停止”成功后没有输出。但是,如果任何一个呼叫失败,我也不会得到任何输出。即使源脚本的返回码不是,systemctl start / stop命令的返回码始终为0。很明显它确实失败了,因为当我运行status命令时它显示为失败。

这是预期的行为吗?除非运行单独的状态命令,否则是否应该没有任何迹象表明发生了故障?如果那不是应该的行为,我如何使它表明发生了故障?

下面的服务文件。

[Unit]
SourcePath=/my/service/script.sh

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/my/service/script.sh start
ExecStop=/my/service/script.sh stop

1 个答案:

答案 0 :(得分:0)

在这里工作正常,CentOS7。也许再次检查一下script.sh是否真的返回非零值?

$pwd
/etc/systemd/system
$cat me.service
[Unit]

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/systemd/system/die.sh
$cat die.sh
#!/bin/bash
echo "dying"
exit 1
$sudo systemctl start me
Job for me.service failed because the control process exited with error code. See "systemctl status me.service" and "journalctl -xe" for details.
$sudo systemctl status me.service
● me.service
   Loaded: loaded (/etc/systemd/system/me.service; static; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2019-09-12 17:55:02 GMT; 8s ago
  Process: 19758 ExecStart=/etc/systemd/system/die.sh (code=exited, status=1/FAILURE)

Sep 12 17:55:02 dpsdev-wkr01 systemd[1]: Starting me.service...
Sep 12 17:55:02 dpsdev-wkr01 die.sh[19758]: dying
Sep 12 17:55:02 dpsdev-wkr01 systemd[1]: me.service: control process exited, code=exited status=1
Sep 12 17:55:02 dpsdev-wkr01 systemd[1]: Failed to start me.service.
Sep 12 17:55:02 dpsdev-wkr01 systemd[1]: Unit me.service entered failed state.
Sep 12 17:55:02 dpsdev-wkr01 systemd[1]: me.service failed.