注意:我写了一篇关于媒体的文章,解释了如何创建服务,以及如何避免这个特殊问题:用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.service
https://unix.stackexchange.com/questions/289629
复制相似问题