title: How can I make sure OS commands run synchronously within Python?
body: <p>I have a python script to rebuild an haproxy configuration and then restart haproxy..  The only problem is that when I run the script from cron, sometimes haproxy gets restarted before the new configuration is in place.</p>
  
  <p>When I run the script from the command line this doesn't happen.</p>
  
  <p>I've tried adding a time.sleep() to the script to have it wait, but sometimes this still happens.  Here's the relevant code:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>How can I make sure the restart waits until the copy completes?</p>