Search code examples
pythontext-to-speechraspbian

gTTS Python Script in background getting tcgetattr(): Inappropriate ioctl for device


Im developing an application on my raspberry Pi 3, using gTTS for Python:

from gtts import gTTS
import os
import threading


def greet_thread(word):
    tts_thread = threading.Thread(target = greet, args=[word])
    tts_thread.start()
def greet(word):
    tts = gTTS(text=word, lang='es')
    tts.save("words.mp3")
    print 'Reproduciendo audio'
    os.system("mpg321 -q presilence.mp3")
    os.system("mpg321 -q words.mp3")

This works perfectly if i run the python script directly from a shell. But if i execute the python script in background using:

python -u script.py > log.txt 2>&1 &

i get this error in my log:

tcgetattr(): Inappropriate ioctl for device

and don't know why. I think is the way it's called from a background process, but no idea how to solved it. Thanks for your attention and help


Solution

  • The problem is that the program needs to be executed using the same user that executes the GUI. So if you are going to execute it in a command shell, avoid using 'root' user.

    In my case i need the program executes on start up too. So i solved it using "auto start" instead of a crontab

    1. Navigate to ~/.config/lxsession/LXDE-pi
    2. nano autostart
    3. Edit the file:

      @lxpanel --profile LXDE-pi

      @pcmanfm --desktop --profile LXDE-pi

      //your script.sh

      //or @python script.py

      @xscreensaver -no-splash

      @point-rpi

    4. Save and exit

    5. Reboot