0

I am trying to execute a non-blocking bash script from python and to get its return code. Here is my function so far:

def run_bash_script(script_fullname, logfile):
    my_cmd = ". " + script_fullname + " >" + logfile +" 2>&1"
    p = subprocess.Popen(my_cmd, shell=True)
    os.waitpid(p.pid, 0)
    print(p.returncode)

As you can see, all the output is redirected into a log file, which I can monitor while the bash process is running.

However, the last command just returns 'None' instead of a useful exit code.

What am I doing wrong here?

1 Answer 1

1

You should use p.wait() rather than os.waitpid(). os.waitpid() is a low level api and it knows nothing about the Popen object so it could not touch p.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it looks like that was exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.