如何确保操作系统命令在Python中同步运行



我有一个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

最新更新