【问题标题】:Why is systemd stopping service immediately after it is started? [closed]为什么systemd在启动后立即停止服务? [关闭]
【发布时间】:2016-04-09 10:57:09
【问题描述】:

我创建了一个 systemd 服务,它应该在启动或重启时调用一个 shell 脚本。

[Unit]
Description=Starts the DCCA index software

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/opt/insiteone/bin/indexControl start
ExecStop=/opt/insiteone/bin/indexControl stop

# Execute pre and post scripts as root
#PermissionsStartOnly=true
Restart=on-abort
TimeoutSec=600

最初它一启动就会无限循环重启,但是当我添加TimeoutSec选项时,它会在服务第一次启动时立即调用ExecStop(启动,然后立即再次停止)。

任何线索,我哪里出错了? P.S:indexControl是一个shell脚本,它启动其他进程。

【问题讨论】:

标签: linux systemd


【解决方案1】:

尝试将Restart=on-abort 更改为Restart=on-abnormal

来自http://www.freedesktop.org/software/systemd/man/systemd.service.html

将此设置为 on-failure 是长时间运行的推荐选择 服务,以通过尝试自动增加可靠性 从错误中恢复。对于应能够终止的服务 他们自己的选择(并避免立即重新启动),on-abnormal 是 另一种选择。

另外,您可能希望将Type=oneshot 添加到[Service] 部分。

来自https://wiki.archlinux.org/index.php/Systemd#Service_types

Type=oneshot:这对于执行单个作业的脚本很有用,然后 出口。您可能还想设置 RemainAfterExit=yes 以便 systemd 进程退出后仍认为服务处于活动状态。

您可以在下面粘贴我建议的更改:

[Unit]
Description=Starts the DCCA index software

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
ExecStart=/opt/insiteone/bin/indexControl start
ExecStop=/opt/insiteone/bin/indexControl stop
Restart=on-abnormal

另外需要考虑的是您是否甚至需要Restart= 行...此服务文件调用的脚本是否经常失败?

【讨论】:

  • 感谢您的回答,但 type=oneshot 不起作用,但 type=forking 起作用。我从这里得到了答案,它有效。 superuser.com/questions/1022142/…
  • 使用 RemainAfterExit=yes 和 Type=oneshot 可能会起作用
  • 不能在 oneshot 服务上设置Restart=on-abnormal,只允许Restart=noService has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.
  • @AkisC 您对 RemainAfterExit=yes 和 Type=oneshot 的建议奏效了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2020-08-22
  • 2016-06-05
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
相关资源
最近更新 更多