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?
asked Feb 19, 2020 at 8:10
Alex
45.1k105 gold badges305 silver badges528 bronze badges
1 Answer 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.
answered Feb 19, 2020 at 9:31
pynexj
21.3k6 gold badges45 silver badges63 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Alex
Thanks, it looks like that was exactly what I needed!
default