Created
April 1, 2020 08:20
-
-
Save olenhad/d27bc29294bc222fae3563441c483279 to your computer and use it in GitHub Desktop.
Running a command infinitely until it succeeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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