注意:我写了一篇关于媒体的文章,解释了如何创建服务,以及如何避免这个特殊问题:用systemd创建Linux服务。
原题:
我使用systemd来让一个工作脚本在任何时候都能工作:
[Unit]
Description=My worker
After=mysqld.service
[Service]
Type=simple
Restart=always
ExecStart=/path/to/script
[Install]
WantedBy=multi-user.target虽然如果脚本在几分钟后正常退出,重启就能正常工作,但我注意到,如果它在启动时多次失败,systemd就会放弃尝试启动它:
Jun 14 11:10:31 localhost systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
Jun 14 11:10:31 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:10:31 localhost systemd[1]: test.service: Failed with result 'exit-code'.
Jun 14 11:10:31 localhost systemd[1]: test.service: Service hold-off time over, scheduling restart.
Jun 14 11:10:31 localhost systemd[1]: test.service: Start request repeated too quickly.
Jun 14 11:10:31 localhost systemd[1]: Failed to start My worker.
Jun 14 11:10:31 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:10:31 localhost systemd[1]: test.service: Failed with result 'start-limit'.类似地,如果我的辅助脚本在退出状态为255时多次失败,systemd将放弃重新启动它的尝试:
Jun 14 11:25:51 localhost systemd[1]: test.service: Failed with result 'exit-code'.
Jun 14 11:25:51 localhost systemd[1]: test.service: Service hold-off time over, scheduling restart.
Jun 14 11:25:51 localhost systemd[1]: test.service: Start request repeated too quickly.
Jun 14 11:25:51 localhost systemd[1]: Failed to start My worker.
Jun 14 11:25:51 localhost systemd[1]: test.service: Unit entered failed state.
Jun 14 11:25:51 localhost systemd[1]: test.service: Failed with result 'start-limit'.systemd在几秒钟后总是重试一次?发布于 2016-11-18 14:15:19
我想把拉胡尔的回答再延长一点。
systemd尝试多次重新启动(StartLimitBurst),如果在StartLimitIntervalSec中达到尝试计数,则停止尝试。这两个选项都属于[unit]部分。
执行之间的默认延迟是100 is (RestartSec),这将导致非常快地达到速率限制。
systemd将不再尝试使用重新启动策略定义对单元进行任何自动重启:
请注意,为
Restart=配置并达到开始限制的单元不再尝试重新启动;但是,它们仍可能在以后的某个点手动重新启动,从那时起,重新启动逻辑将再次被激活。
Rahul的回答很有帮助,因为较长的延迟会阻止在StartLimitIntervalSec时间内到达错误计数器。正确的答案是将RestartSec和StartLimitBurst设置为合理的值。
发布于 2016-06-14 09:40:56
是的,有。可以在x秒之后在[Service]部分下指定重试,
[Service]
Type=simple
Restart=always
RestartSec=3
ExecStart=/path/to/script保存文件后,需要重新加载守护进程配置,以确保systemd知道新文件,
systemctl daemon-reload然后重新启动服务以启用更改,
systemctl restart test正如你所要求的,看看文件,
Restart=on-failure听起来是个不错的推荐。
发布于 2022-01-17 10:09:52
如果您的服务在reboot之后没有重新启动,请确保您在-之前启用了它:
sudo systemctl enable your.servicehttps://unix.stackexchange.com/questions/289629
复制相似问题