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?

1
  • Have you tried try/except Commented Jan 31, 2019 at 10:48

1 Answer 1

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") 
Sign up to request clarification or add additional context in comments.

3 Comments

and what it does?
Gets the error's stacktrace. But I realized that you just return when the returncode != 0... No wonder that it "just ends"
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.