在某些特殊情况下,我们需要频繁重启服务。因此,当服务因启动限制而失败时,建议的解决方案是调用“reset-failed”。我们发现,即使在调用 reset-failed 之后,紧接着的启动也会因启动限制而失败。唯一的解决方法是在调用启动之前暂停 6 秒(睡眠 6),如果前一个启动失败的话。
为了测试目的,我在每次启动前都调用了 reset-failed,但仍然会因启动限制而失败。
admin@vlab-03:~/tmp$ cat t.py
#! /usr/bin/env python3
import os
import sys
def _service_restart(svc_name):
rc_stop = os.system(f"sudo systemctl stop {svc_name}")
rc_reset = os.system(f"sudo systemctl reset-failed {svc_name}")
rc_start = os.system(f"sudo systemctl start {svc_name}")
print(f"rc_stop={rc_stop} rc_reset={rc_reset} rc_start={rc_start}")
if rc_start != 0:
print("Exiting ...")
sys.exit(-1)
def main():
for i in range(10):
print(f"-------------------i = {i} ---------------")
_service_restart("rsyslog-config")
print("done")
if __name__ == "__main__":
main()
admin@vlab-03:~/tmp$
o/p:
admin@vlab-03:~/tmp$ ./t.py
-------------------i = 0 ---------------
rc_stop=0 rc_reset=0 rc_start=0
-------------------i = 1 ---------------
rc_stop=0 rc_reset=0 rc_start=0
-------------------i = 2 ---------------
rc_stop=0 rc_reset=0 rc_start=0
-------------------i = 3 ---------------
rc_stop=0 rc_reset=0 rc_start=0
-------------------i = 4 ---------------
rc_stop=0 rc_reset=0 rc_start=0
-------------------i = 5 ---------------
Job for rsyslog-config.service failed because the control process exited with error code.
See "systemctl status rsyslog-config.service" and "journalctl -xe" for details.
rc_stop=0 rc_reset=0 rc_start=256
Exiting ...
任何建议都会非常有帮助。
admin@vlab-03:~$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
admin@vlab-03:~$ uname -a
Linux vlab-03 5.10.0-8-2-amd64 #1 SMP Debian 5.10.46-4 (2021-08-03) x86_64 GNU/Linux