为什么systemd在服务启动后立即停止?

14

我创建了一个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(启动后立即停止)。

请问我做错了什么? 附注:indexControl是一个启动其他进程的Shell脚本。


chkconfig index off 也没有帮助(index.service 是 systemd 服务文件) - RajSanpui
1
我在这里找到了答案:http://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started,解决了问题。 - RajSanpui
1个回答

7

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

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

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

此外,您可能还想在[Service]部分中添加Type=oneshot

来自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=这一行......这个服务文件调用的脚本是否经常失败?


5
谢谢你的回答,但 type=oneshot 无效,而 type=forking 可以。我从这里找到了可行的解决方法:http://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started/。 - RajSanpui
3
使用 "RemainAfterExit=yes" 和 "Type=oneshot" 或许可以解决问题。 - AkisC
2
无法在一次性服务上设置“Restart=on-abnormal”,只允许使用“Restart=no”: 服务具有除“no”以外的Restart=设置,这对于Type=oneshot服务是不允许的。拒绝。 - s1moner3d
1
@AkisC,你的建议使用RemainAfterExit=yes和Type=oneshot很有效!谢谢。 - Cardin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接