flush output

This will ensure that the stderr and stdout are running synchroniously.
The output in journalctl will now make more sense.
This commit is contained in:
Peter Leurs 2024-12-29 16:49:00 +01:00
parent 1e96c4e5ee
commit 7756236e1c

View file

@ -47,7 +47,11 @@ def run_command(command, env=None):
current_env = os.environ.copy()
if env is not None:
current_env.update(env)
sys.stdout.flush()
sys.stderr.flush()
process = subprocess.run(command, shell=True, env=current_env)
sys.stdout.flush()
sys.stderr.flush()
if process.returncode != 0:
raise RuntimeError(f"Command '{command}' failed with return code {process.returncode}, environment is {env}")
@ -55,8 +59,12 @@ def get_command(command, env=None, full_return=False):
current_env = os.environ.copy()
if env is not None:
current_env.update(env)
sys.stdout.flush()
sys.stderr.flush()
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=current_env)
output, error = process.communicate()
sys.stdout.flush()
sys.stderr.flush()
return_code = process.poll()
if full_return:
return output.decode().strip(), error.decode().strip(), return_code