如何确保OS命令在Python中同步运行?

时间:2013-07-15 16:01:32

标签: python operating-system command synchronous os.system

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

如何确保重启等待复制完成?

1 个答案:

答案 0 :(得分:2)

很确定这应该这样做。

commands = [ ... ]
for command in commands:
    if os.system(command) == 0:
        # Check for failure and wait
        continue
    else:
        print "ERROR"
        break
相关问题