Alex
Alex

Reputation: 44305

How to execute a non-blocking script in python and get its return code?

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?

Upvotes: 0

Views: 222

Answers (1)

pynexj
pynexj

Reputation: 20688

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.

Upvotes: 1

Related Questions