title: python subprocess stdin.write(pwd) IOError: [Errno 32] Broken pipe
body: <p>I am trying to write the stdout and stderr to file and input the password for the sudo prompt which is stored in a string. Getting the broken pipe error in the err file when trying to execute it in the background in below way.</p>
  
  <p>cmd.py</p>
  
  <pre><code>def preexec_function():
      import os
      import signal
      # Detaching from the parent process group
      os.setpgrp()
      # Explicitly ignoring signals in the child process
      signal.signal(signal.SIGINT, signal.SIG_IGN)
  
  cmd = "python pexecutor.py"
  p = Popen(cmd, close_fds=True, stdout=None, stderr=None, stdin=None,
            preexec_fn=preexec_function)
  </code></pre>
  
  <p>pexecutor.py</p>
  
  <pre><code>from subprocess import Popen, PIPE
  import os
  command="sudo yum -y install postgresql.x86_64"
  stdin_str="myrootpwd"
  std_out_file = open("out.txt", 'a+')
  std_err_file = open("err.txt", 'a+')
  process = Popen(command, stdout=std_out_file, stderr=std_err_file, 
                  stdin=PIPE)
  import time
  time.sleep(5)
  pwd = b'{}'.format(stdin_str + str(os.linesep))
  process.stdin.write(pwd)
  process.stdin.flush()
  data = process.communicate()
  </code></pre>
  
  <p>Getting the error: </p>
  
  <pre><code>Traceback (most recent call last):
      File "pexecutor.py", line 10, in execute
    process.stdin.write(pwd)
  IOError: [Errno 32] Broken pipe
  </code></pre>
  
  <p>OS : CentOS</p>
  
  <p>Python Version : 2.7.5</p>