python subprocess communicate hangs calling shell script

3.3k Views Asked by At

Using python 3.2, and the following code snippet:

p = subprocess.Popen(['../start_server.sh'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = p.communicate()

if out != None :
   out = out.decode('utf-8')
if err != None :
   err = err.decode('utf-8')

print('out ',out)
print('err ',err)

on some shell scripts, it works just fine and I get my output. on others it just hangs. but in every case the shell script runs from the command line with no errors. The only commonality i can see is (usually) the ones that hang have zero output. When stuff fails, I check running processes and i see my shell script is not listed and the python script is still running

Whats a reliable way to call a shell script and always return control to my python program?


Edit:

Using pipes Popen and such is not a requirement, the only requirement is that control is returned to my python script when the shell script exits. If the shell script never returns to the command prompt, then my python script will also never return.

So assuming the shell script(s) I am calling always return to the command prompt, how can I get control back to my python program?
If theres a better way that what ive listed above -- please enlighten me

One additional bit ive found is the shell scripts that "hang" seem to end with a call to 'nohup' Ye they return to the command prompt with no issues.

1

There are 1 best solutions below

2
On

Whats a reliable way to call a shell script and always return control to my python program?

If you are using pipes, this will depend on your scripts; a more general answer is essentially the halting problem and even the mighty StackOverflow can't help you with that.

I would encourage you to dig deeper and try to create a reproducible case so that we can help you solve the particular problem you're seeing.

Edit

If you don't need pipes, then just omit the stdout and stderr parameters (or set them to something other than PIPE). See python subprocess management.