title: Python Popen process not working when it is executed from init.d script
body: <p>I have a script stored in /etc/init.d that works ok when executed at boot time. Since I do not export USER, it should execute everything as root (if I'm not wrong).</p>
  
  <p>Then, it executes python3 script.py
  In this script, everything works fine until it reaches</p>
  
  <pre><code>espeak_process = Popen(["espeak", "-ves", "-s100", msg, "--stdout"], stdout=subprocess.PIPE)
  aplay_process = Popen(["aplay", "-D", "sysdefault"], stdin=espeak_process.stdout, stdout=subprocess.PIPE)
  </code></pre>
  
  <p>EDIT: changed for this code, but the created file is empty</p>
  
  <pre><code>def log_uncaught_exceptions(ex_cls, ex, tb):
      f = open('/home/pi/debug_err.txt', 'w')
      f.write('hi ')
      f.write(ex_cls+' '+ex+' '+tb)
      f.close()
  
  sys.excepthook = log_uncaught_exceptions
  espeak_process = Popen(["espeak", "-ves", "-s100", msg, "--stdout"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  </code></pre>
  
  <p>/EDIT</p>
  
  <p>At which point it stops. I can tell that since it writes a debug file before and after this step, and the 2nd one is never written.</p>
  
  <p>The thing though is that if I execute in a terminal</p>
  
  <pre><code>sudo sh /etc/init.d/begin start
  </code></pre>
  
  <p>Then the "python3 script.py" is executed entirely ok, including the Popen part.</p>
  
  <p>Why is that happening?
  Thx</p>
  
  <p>(init.d script)</p>
  
  <pre><code>#! /bin/sh
  # /etc/init.d/begin 
  
  #USER=pi
  HOME=/home/pi
  
  #export USER HOME
  export HOME
  
  case "$1" in
   start)
    if [ -f "/home/pi/begin.pid" ];
    then
      kill -9 $(cat /home/pi/begin.pid)
      rm -f /home/pi/begin.pid
    fi
    python3 /home/pi/script.py &amp;
    ;;
  
   stop)
    kill -9 $(cat /home/pi/begin.pid)
    rm -f /home/pi/begin.pid
    ;;
  
   *)
    echo "Usage: /etc/init.d/begin {start|stop}"
    exit 1
    ;;
  esac
  
  exit 0
  </code></pre>