I tried to restrict the number of a service (in a container) restart. The OS version is centos-release-7-5, the service file is pretty much as below (removed some parameters for reading convenience). It should be pretty straight forward as some other posts pointed out (Post of Server Fault restart limit 1 , Post of Stack Overflow restart limit 2 ). Yet StartLimitBurst and StartLimitIntervalSec never works for me.
I tested with several ways: (1) I check the service PID, kill the service with “kill -9 ****” several times. The service always gets restarted after 20s! (2) I also tried to mess up the service file, make the container never runs. Still, it doesn’t work, the service file just keep restarting.
Any idea?
[Unit] Description=Hello Fluentd After=docker.service Requires=docker.service StartLimitBurst=2 StartLimitIntervalSec=150s [Service] EnvironmentFile=/etc/environment ExecStartPre=-/usr/bin/docker stop "fluentd" ExecStartPre=-/usr/bin/docker rm -f "fluentd" ExecStart=/usr/bin/docker run fluentd ExecStop=/usr/bin/docker stop "fluentd" Restart=always RestartSec=20s SuccessExitStatus=143 [Install] WantedBy=multi-user.target
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
StartLimitIntervalSec= was added as part of systemd v230. In systemd v229 and below, you can only use StartLimitInterval=. You will also need to put StartLimitInterval= and StartLimitBurst= in the [Service] section – not the [Unit] section.
To check your systemd version on CentOS, run rpm -q systemd.
If you ever upgrade to systemd v230 or above, the old names in the [Service] section will continue to work.
Source: https://lists.freedesktop.org/archives/systemd-devel/2017-July/039255.html
You can have this problem without seeing any error at all, because systemd ignores unknown directives. systemd assumes that many newer directives can be ignored and still allow the service to run.
It is possible to manually check a unit file for unknown directives. At least it seems to work on recent systemd:
$ systemd-analyze verify foo.service /etc/systemd/system/foo.service:9: Unknown lvalue 'FancyNewOption' in section 'Service'
Method 2
I think I found the issue. All the doc online suggests those all parameters are in UNIT file (systemd unit file), but still in my system (centos 7.5), they are in service file. Besides the name is “StartLimitInterval”, not “StartLimitIntervalSec”.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0