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).
Then, it executes python3 script.py In this script, everything works fine until it reaches
espeak_process = Popen(["espeak", "-ves", "-s100", msg, "--stdout"], stdout=subprocess.PIPE)
aplay_process = Popen(["aplay", "-D", "sysdefault"], stdin=espeak_process.stdout, stdout=subprocess.PIPE)
EDIT: changed for this code, but the created file is empty
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)
/EDIT
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.
The thing though is that if I execute in a terminal
sudo sh /etc/init.d/begin start
Then the "python3 script.py" is executed entirely ok, including the Popen part.
Why is that happening? Thx
(init.d script)
#! /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 &
  ;;
 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