Skip to content

Instantly share code, notes, and snippets.

@olenhad
Created April 1, 2020 08:20
Show Gist options
  • Save olenhad/d27bc29294bc222fae3563441c483279 to your computer and use it in GitHub Desktop.
Save olenhad/d27bc29294bc222fae3563441c483279 to your computer and use it in GitHub Desktop.
Running a command infinitely until it succeeds
import subprocess
import sys
def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
output = process.stdout.read(1).decode()
if output == '' and process.poll() is not None:
break
if output != '':
sys.stdout.write(output)
sys.stdout.flush()
return process.returncode
while True:
code = run_command(sys.argv[1:])
if code == 0:
break
else:
print("Retrying as previously failed with %s" % code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment