Skip to content

Instantly share code, notes, and snippets.

@meteozond
Created December 6, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meteozond/9ce6732eb3594072bf865da87e90957a to your computer and use it in GitHub Desktop.
Save meteozond/9ce6732eb3594072bf865da87e90957a to your computer and use it in GitHub Desktop.
import os
import sys
from subprocess import PIPE, STDOUT, Popen
bash_commands = sys.argv[1:]
procs = []
for n, cmd in enumerate(bash_commands):
kwargs = {
'stdout': PIPE,
'env': os.environ.copy()
}
if n == 0:
kwargs['stdin'] = PIPE
proc = Popen(['bash', '-c', cmd], **kwargs)
procs.append(proc)
exited = 0
while exited < len(procs) - 1:
exited = 0
for n, proc in enumerate(procs):
returncode = proc.poll()
if returncode is not None:
exited += 1
if returncode > 0:
for sproc in procs:
if sproc.returncode is None:
sproc.terminate()
sys.exit(returncode)
next_proc = None
if n + 1 < len(procs):
next_proc = proc[n]
for line in iter(proc.stdout.readline, b''):
if next_proc:
next_proc.communicate(input=line)
else:
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment