我有一个python脚本来重建haproxy配置,然后重新启动haproxy。.唯一的问题是,当我从cron运行脚本时,有时haproxy在新配置到位之前会重新启动。
当我从命令行运行脚本时,不会发生这种情况。
我尝试将time.sleep()添加到脚本中以使其等待,但有时仍会发生这种情况。以下是相关代码:
command = "/home/adam/bin/genproxy.sh"
os.system(command)
os.system("cp /home/adam/bin/haproxy.cfg /etc/haproxy/")
time.sleep(2)
os.system("sudo /etc/init.d/haproxy restart")
如何确保重启等待复制完成?
最佳答案
可以肯定,这应该做到。
commands = [ ... ]
for command in commands:
if os.system(command) == 0:
# Check for failure and wait
continue
else:
print "ERROR"
break