我已经阅读了 systemd 手册页以及一些与以下内容相关的问答StartLimit*
我尝试了针对新版本推荐的操作:
[Unit]
StartLimitBurst=5
StartLimitIntervalSec=90
[Service]
RestartSec=5
Restart=always
此外,对于旧版本应该做些什么systemd
:
[Service]
StartLimitBurst=5
StartLimitInterval=90
...
RestartSec=5
Restart=always
(这仍然被接受并且systemd-analyze
没有显示任何错误)
但这两种方法都不起作用!也就是说,我的守护进程不断无限期地重新启动,我NRestarts
还可以看到显示正确的计数。
systemd 237
我使用的是 Ubuntu (Bionic)版本18.04
。我不介意使用任何一种格式,但我想确保重启尝试次数不超过 5 或 6 次。
答案1
我认为您分享的配置有效。您可能需要分享有关该服务的更多详细信息。
以下是我运行的测试步骤。我使用的是 Ubuntu 18.04(systemd 237-3ubuntu10.53
)。
创建一个基本的 shell 脚本以作为服务运行。
cat <<EOF > /usr/local/bin/myservice.sh
#!/usr/bin/env bash
sleep 1
exit 0
EOF
创建基础服务。
cat <<EOF > /etc/systemd/system/my.service
[Unit]
StartLimitBurst=5
StartLimitIntervalSec=90
[Service]
ExecStart=/bin/bash /usr/local/bin/myservice.sh
RestartSec=5
Restart=always
EOF
启动服务。
systemctl daemon-reload
systemctl start my.service
日志表明该服务已启动 5 次,然后进入失败状态。
systemd[1]: Started my.service.
systemd[1]: my.service: Service hold-off time over, scheduling restart.
systemd[1]: my.service: Scheduled restart job, restart counter is at 1.
systemd[1]: Stopped my.service.
systemd[1]: Started my.service.
systemd[1]: my.service: Service hold-off time over, scheduling restart.
systemd[1]: my.service: Scheduled restart job, restart counter is at 2.
systemd[1]: Stopped my.service.
systemd[1]: Started my.service.
systemd[1]: my.service: Service hold-off time over, scheduling restart.
systemd[1]: my.service: Scheduled restart job, restart counter is at 3.
systemd[1]: Stopped my.service.
systemd[1]: Started my.service.
systemd[1]: my.service: Service hold-off time over, scheduling restart.
systemd[1]: my.service: Scheduled restart job, restart counter is at 4.
systemd[1]: Stopped my.service.
systemd[1]: Started my.service.
systemd[1]: my.service: Service hold-off time over, scheduling restart.
systemd[1]: my.service: Scheduled restart job, restart counter is at 5.
systemd[1]: Stopped my.service.
systemd[1]: my.service: Start request repeated too quickly.
systemd[1]: my.service: Failed with result 'start-limit-hit'.
systemd[1]: Failed to start my.service.