Running the following command: yes | head -1
, from the shell, outputs y
. Using python's subprocess module to call it with a shell hangs indefinitely:
import subprocess
arg = "yes | head -1"
process = subprocess.Popen(arg,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print "Command started: %d" % process.pid
r = process.communicate()
print "Command ended: %s %s" % r
Killing the process exogenously using kill
does not help, nor does making the child process it's session leader using preexec_fn=os.setsid
.
What could be causing this behavior, and is there anyway I can prevent it?
I'm running python 2.7.3 and my /bin/sh
is GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)