0

I wrote python script to run bash script it runs it with this line:

result = subprocess.Popen(['./test.sh %s %s %s' %(input_file, output_file, master_name)], shell = True)

if result != 0:
    print("Sh*t hits the fan at some point")
    return
else:
    print("Moving further")

Now I having troubles when bash script fails, python doesn't continue doing what it was doing it just ends. How can I make it so that python script would continue running after bash fails?

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
LeTadas
  • 3,436
  • 9
  • 33
  • 65

1 Answers1

1

You forgot about the communicate. Besides you return when the bash script fails so no wonder the python "just stops".

from subprocess import Popen, PIPE

p = Popen(..., stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode != 0: 
   print("Sh*t hits the fan at some point %d %s %s" % (p.returncode, output, error))
print("Movign further")
Colonder
  • 1,556
  • 3
  • 20
  • 40
  • and what it does? – LeTadas Jan 31 '19 at 10:50
  • Gets the error's stacktrace. But I realized that you just `return` when the `returncode != 0`... No wonder that it "just ends" – Colonder Jan 31 '19 at 10:52
  • 1
    As the documentation already tells you, don´t use bare `Popen` if you can use one of the higher-level functions which take care of exactly these details for you. In this case, `check_call` or `run(..., check=True)` will raise an exception if the subprocess fails. – tripleee Jan 31 '19 at 10:53