2016-01-05 112 views
5

我创建了一个systemd服务,它应该在启动或重启时调用shell脚本。为什么systemd在启动后立即停止服务?

[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脚本,它启动其他进程。

+0

'chkconfig index off'也没有帮助(index.service是ssystemd服务文件) – kingsmasher1

+0

我在这里得到了答案:http://superuser.com/questions/1022142/why-is-systemd-stopping -service-immediately-after-it-is-started修复了问题 – kingsmasher1

回答

3

尝试改变Restart=on-abortRestart=on-abnormal

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

这个设置对故障是建议选择长期运行 服务,以便通过尝试从自动 复苏以增加可靠性错误。对于能够终止于 自己选择的服务(并且避免立即重新启动),对于异常是 的替代选择。

此外,您可能需要将Type=oneshot添加到[Service]部分。

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

类型=单冲:这是为做一个工作,然后 退出脚本很有用。您可能还需要设置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/ – kingsmasher1

+0

With RemainAfterExit = yes and Type = oneshot也许可以工作 – AkisC

+1

无法设置Restart = on在** oneshot **服务上的“异常”,只允许'Restart = no'被允许: '服务有重启=设置不是no,Type = oneshot服务不允许。 Refusing.' – s1moner3d