首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Systemd Restart=always不受尊重

Systemd Restart=always不受尊重
EN

Unix & Linux用户
提问于 2016-06-14 09:21:06
回答 4查看 216.1K关注 0票数 102

注意:我写了一篇关于媒体的文章,解释了如何创建服务,以及如何避免这个特殊问题:用systemd创建Linux服务

原题:

我使用systemd来让一个工作脚本在任何时候都能工作:

代码语言:javascript
运行
复制
[Unit]
Description=My worker
After=mysqld.service

[Service]
Type=simple
Restart=always
ExecStart=/path/to/script

[Install]
WantedBy=multi-user.target

虽然如果脚本在几分钟后正常退出,重启就能正常工作,但我注意到,如果它在启动时多次失败,systemd就会放弃尝试启动它:

代码语言:javascript
运行
复制
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将放弃重新启动它的尝试:

代码语言:javascript
运行
复制
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在几秒钟后总是重试一次?

EN

回答 4

Unix & Linux用户

回答已采纳

发布于 2016-11-18 14:15:19

我想把拉胡尔的回答再延长一点。

systemd尝试多次重新启动(StartLimitBurst),如果在StartLimitIntervalSec中达到尝试计数,则停止尝试。这两个选项都属于[unit]部分。

执行之间的默认延迟是100 is (RestartSec),这将导致非常快地达到速率限制。

systemd将不再尝试使用重新启动策略定义对单元进行任何自动重启:

请注意,为Restart=配置并达到开始限制的单元不再尝试重新启动;但是,它们仍可能在以后的某个点手动重新启动,从那时起,重新启动逻辑将再次被激活。

Rahul的回答很有帮助,因为较长的延迟会阻止在StartLimitIntervalSec时间内到达错误计数器。正确的答案是将RestartSecStartLimitBurst设置为合理的值。

票数 100
EN

Unix & Linux用户

发布于 2016-06-14 09:40:56

是的,有。可以在x秒之后在[Service]部分下指定重试,

代码语言:javascript
运行
复制
[Service]
Type=simple
Restart=always
RestartSec=3
ExecStart=/path/to/script

保存文件后,需要重新加载守护进程配置,以确保systemd知道新文件,

代码语言:javascript
运行
复制
systemctl daemon-reload

然后重新启动服务以启用更改,

代码语言:javascript
运行
复制
systemctl restart test

正如你所要求的,看看文件,

代码语言:javascript
运行
复制
Restart=on-failure

听起来是个不错的推荐。

票数 67
EN

Unix & Linux用户

发布于 2022-01-17 10:09:52

如果您的服务在reboot之后没有重新启动,请确保您在-之前启用了它:

代码语言:javascript
运行
复制
sudo systemctl enable your.service
票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/289629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档