Reputation: 29
I've created a master.py
to kick off other scripts.
from subprocess import PIPE, STDOUT, run
def main():
command = ["python3", "file1.py"]
print("Executing: {}".format(command))
exit_code, output_string = run_command(command)
print(output_string)
command = ["python3", "file2.py"]
print("Executing: {}".format(command))
exit_code, output_string = run_command(command)
print(output_string)
command = ["python3", "file3.py"]
print("Executing: {}".format(command))
exit_code, output_string = run_command(command)
print(output_string)
print("Exiting master.py with status_code of {} because {NAME_OF_FILE_THAT_FAILED} failed.")
def run_command(command_to_run):
result = run(command_to_run, stdout=PIPE, stderr=STDOUT)
return result.returncode, result.stdout.decode()
if __name__ == "__main__":
main()
I'm trying to capture sys.exit()
of each sub process script, i.e, capture what file1.py
, file2.py
and file3.py
exited with. If all jobs passed, then the master.py
will exist with 0
but if at least 1 sub job failed and exited with 1
then I want the master job to exit with 1 as well.
Once it captures the sys.exit
from all the sub jobs, based on their results, I would like to print that error on the master's final output (the print statement)
Upvotes: 2
Views: 2536
Reputation: 137
If a subprocess exits with status code 1, the value will be stored result.returncode
. You can get the error message by also returning result.stderr.decode()
in run_command()
For the behaviour of master.py
after each child process terminates, I believe you can simply control it using if else statements.
Upvotes: 1