我有这个服务:
[Unit]
Description=TimeAgent Recorder
Requires=timeagent.service
[Service]
User=deployer
Group=sudo
WorkingDirectory=/home/deployer/timeagent
Environment=HOME=/home/deployer
Environment=RAILS_ENV=production
Environment=EYE_CLIENT_TIMEOUT=120
Restart=always
RestartSec=15
Nice=1
Type=forking
PIDFile=/home/deployer/.eye/pid
ExecStart=/usr/local/bin/bundle exec eye load recorder.eye
ExecStop=/usr/local/bin/bundle exec eye quit --stop-all -t 600
[Install]
WantedBy=timeagent.service
重启后,有时无法启动:
$ systemctl status timeagent-recorder
● timeagent-recorder.service - TimeAgent Recorder
Loaded: loaded (/etc/systemd/system/timeagent-recorder.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2017-03-20 16:30:17 GMT; 42min ago
Process: 10929 ExecStop=/usr/local/bin/bundle exec eye quit --stop-all -t 600 (code=exited, status=0/SUCCESS)
Process: 11409 ExecStart=/usr/local/bin/bundle exec eye load recorder.eye (code=killed, signal=TERM)
Main PID: 9236 (code=killed, signal=TERM)
Mar 20 16:30:16 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 20 16:30:17 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 20 16:30:17 Ti systemd[1]: timeagent-recorder.service: Start request repeated too quickly.
Mar 20 16:30:17 Ti systemd[1]: Failed to start TimeAgent Recorder.
Mar 20 16:30:20 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 20 16:30:20 Ti systemd[1]: timeagent-recorder.service: Start request repeated too quickly.
Mar 20 16:30:20 Ti systemd[1]: Failed to start TimeAgent Recorder.
Mar 20 16:30:20 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 20 16:30:20 Ti systemd[1]: timeagent-recorder.service: Start request repeated too quickly.
Mar 20 16:30:20 Ti systemd[1]: Failed to start TimeAgent Recorder.
这也是一些相关的输出:
$ systemctl show timeagent-recorder | grep -i restart
Restart=always
RestartUSec=15s
我确保启用该服务以及 daemon-reload,正如您在上面看到的那样,它已启用并且 systemd 尝试启动它,但完全忽略 RestartSec。
我使用的是 Ubuntu 16.04.1 LTS,systemd 版本为 229-4ubuntu10。
知道为什么 RestartSec=15 没有得到遵守吗? - 我还尝试使用 RestartSec=15s 这也没有什么区别。
编辑:
我已按照建议将“需要”更改为“想要”,但问题仍然发生。这是相关日志,更多地显示了正在发生的情况:
# journalctl -f -u timeagent-recorder
Mar 26 20:18:21 Ti bundle[7030]: Config loaded!
Mar 26 20:18:21 Ti systemd[1]: Started TimeAgent Recorder.
Mar 26 22:17:06 Ti systemd[1]: Stopping TimeAgent Recorder...
Mar 26 22:17:08 Ti bundle[22989]: Eye quit ಠ╭╮ಠ (/home/deployer)
Mar 26 22:17:08 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:08 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 26 22:17:09 Ti bundle[23272]: Eye started! ㋡ (/home/deployer)
Mar 26 22:17:09 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:09 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 26 22:17:09 Ti systemd[1]: timeagent-recorder.service: Control process exited, code=exited status=1
Mar 26 22:17:09 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:09 Ti systemd[1]: timeagent-recorder.service: Unit entered failed state.
Mar 26 22:17:09 Ti systemd[1]: timeagent-recorder.service: Failed with result 'exit-code'.
Mar 26 22:17:09 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 26 22:17:09 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:09 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 26 22:17:10 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:10 Ti systemd[1]: Starting TimeAgent Recorder...
Mar 26 22:17:10 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:10 Ti systemd[1]: timeagent-recorder.service: Start request repeated too quickly.
Mar 26 22:17:10 Ti systemd[1]: Failed to start TimeAgent Recorder.
Mar 26 22:17:10 Ti systemd[1]: Stopped TimeAgent Recorder.
Mar 26 22:17:10 Ti systemd[1]: timeagent-recorder.service: Start request repeated too quickly.
Mar 26 22:17:10 Ti systemd[1]: Failed to start TimeAgent Recorder.
答案1
我相信你所经历的行为是因为你的Requires=
依赖。根据以下Requires=
部分man systemd.unit
:
如果其他单位之一被停用或激活失败,则该单位将被停用。
听起来这就是这里发生的事情。在本段后面,有这样的建议,我认为它适用于这种情况:
通常,使用 Wants= 而不是 Requires= 是更好的选择,以便使系统在处理故障服务时更加健壮。
的文档Wants=
说:
Requires= 的较弱版本。如果配置单元已启动,则将启动此选项中列出的单元。然而,如果列出的单位未能启动或无法添加到交易中,这不会影响整个交易的有效性。这是将一个单元的启动与另一个单元的启动挂钩的推荐方法。
用另一种方式回答:我不认为“RequireSec=”逻辑有机会启动,因为 systemd 由于不满足“Require=”条件而取消了服务。
这是我的猜测。