mpg123 and ffplay python scripts work from command line but not as systemd service

65 Views Asked by At

I'm trying to set up a daemon to play short sound effects as part of an interactive wheelchair ride. I'm using an Asus tinker board R2 with debian Buster. This is a steep learning curve in both Debian and Python. My strategy (grateful for ideas!) is to have the main control routine load a simple value into a text file when a sound effect is needed. The sound routine polls the file every 0.5 second and if a valid value is found it plays the relevant mp3 sound file and resets the "semaphore". The idea being this is to avoid my main routine being blocked while the sound is playing. If I call the playsound loop from a command line (python3 /home/scripts/runsound.py) it works beautifully, playse the sound and clears the semaphore. If I start runsound.py as a systemd service, it runs, and clears the semaphone, but no sound is produced. I've tried a few different strategies to initiate the sound action and they all do the same thing.

Systemd Configuration:

[Unit]
Description=plays carousel sound effects

[Service]
PATH=/home/carousel/sounds
User=root
Group=root
ExecStart=python3 /home/carousel/scripts/runsound.py
Restart=always

[Install]
WantedBy=multi-user.target

runsound.py:

# command line is runsound.py
# run as a service on startup - runsound.service - plays selected sound track
import time
import os
import subprocess

try:
    while True:
        f = open("/home/carousel/scripts/SoundTrack.txt", "r")
        # read file for sound track value. Capture error if file is not correct and return 0 instead of crashing
        try:
            SoundNum = int(f.readline())
        except ValueError:
            SoundNum = 0
        f.close() 
        if SoundNum == 1:
            file = "/home/carousel/sounds/1_startup.mp3"
        elif SoundNum == 2:
            file = "/home/carousel/sounds/2_countin.mp3"
        elif SoundNum == 3:
            file = "/home/carousel/sounds/3_countin.mp3"
        elif SoundNum == 4:
            file = "/home/carousel/sounds/4_bell.mp3"
        elif SoundNum == 5:
            file = "/home/carousel/sounds/5_finished.mp3"
        elif SoundNum == 9:
            file = "/home/carousel/sounds/9_alarm.mp3"
        if SoundNum > 0:
        
# All methods below play the selected sound file when this program is started from the command line, but not when stared as a systemd service.
            result = subprocess.run(["sudo ffplay " + file +" -autoexit -nodisp -vn -acodec mp3"], shell=True, capture_output=True, text=True)
#            result = subprocess.Popen(["mpg123 " + file], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
#            result = subprocess.run(["sudo mpg123 -q " + file], shell=True, capture_output=True, text=True)
#            print(result.stdout)
#            os.system("sudo mpg123 -q " + file) # sudo appears to be essential.
            print(file)
        
# clear trigger file by resetting value to 0
            f = open("/home/carousel/scripts/SoundTrack.txt", "w")  # w=replace file contents
            f.write(str(0)+"\n")   # Write 0 to clear pending sound
            f.close()
        time.sleep(0.5)  # 500ms
except KeyboardInterrupt:
        SoundNum = 0

When run from command line, it is still essential that the mp3 player is called by sudo. Without that, it behaves the same as when run as a service. I'm sure that's a clue as to what's going on... I've tried setting user=root, but no change to the service. I'm very happy to try any theories and report back. Thanks.

I've noticed a similar(?) issue running another python3 script and mysql connector. When run as a service it failed, saying there was no mysql connector, but woked fine from the command line. Changing the systemd definition to User=james fixed this. Pip3 has installed the mysql connector in /home/james/.local/lib/python3.7/site-packages. I tried changing the user for runsound.service but it didn't help. I think there are more clues here...

0

There are 0 best solutions below