I use supervisor to run about 6 startup scripts. The services start fine. But I then noticed that the scripts that had to do with calling a wav sound file did not work.

checking the status of supervisor I see that all my scripts are running yet the wav scripts fail to launch.

Here's an example of one of the python scripts

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO
import time
import os
from subprocess import Popen


song_path = '/home/pi/alerts/buzzer_alert.wav'
# change it to desire duration in seconds
duration_time = 5

def RCtime(RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)

GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while GPIO.input(RCpin) == GPIO.LOW:
    reading += 1
return reading

def main():
global duration_time
global song_path

while True:
    print(RCtime(27))  # Read RC timing using pin #17
    if RCtime(27) > 2700:
        print('Laser triped')
        os.system('aplay -d {} {}'.format(duration_time, song_path))



if __name__ == '__main__':
  • Please be more verbatim about your issue and share your code - or even better a Short, Self Contained, Correct (Compilable), Example (see also MCVE) to get a helpful answer. – Ghanima May 4 '18 at 20:54
  • I have edited the question to show an example of a python script that did not launch with supervisor – Allen Jakpa May 5 '18 at 5:31
  • What you mean is you think it failed to launch because the sound did not get played. Does 'laser tripped' ever get printed wherever the output from this goes? Are you capturing standard error output at all? Very possibly the issue is that it can't access the audio device from the background. – goldilocks May 5 '18 at 13:33
  • @goldilocks Yes. that is what i meant to say. No, there is no standard error output what so ever. I see no errors anywhere – Allen Jakpa May 5 '18 at 19:09
  • 1
    "No, there is no standard error output what so ever." -> How do you know that? How are you (in theory) capturing it? If you can't be bothered to include these kinds of details people will just skip over this kind of question. There are just too many that boil down to "I tried to run a background script via init/cron/whatever but it didn't work. What could be wrong? It works if I run it from the command line!" Anything could be wrong. Or nothing. Who knows. Remember, it is your problem and don't expect other people to keep trying to drag info out of you. It's tedious. – goldilocks May 5 '18 at 19:28

Under Linux, the audio device is ‘attached’ to the display screen. The screen typically belongs to the user, not the superuser, so attempts to output audio as root will fail. Either:

  1. investigate whether you need to run this startup script as root: most GPIO works fine under user control now, and it's relatively easy to create user systemd services (the files go in ~/.config/systemd/user/); or

  2. set the environment variable DISPLAY=:0 for your script if you're running a GUI, and the sound will play.

  • 1.)I do not need root privilege to run the script. Kindly explain when you mean by "(the files go in ~/.config/systemd/user/". 2.) I am running the pi headless so i do not normally use the GUI. – Allen Jakpa May 5 '18 at 19:13
  • @AllenJakpa the systemd service files go in the folder ~/.config/systemd/user/ when you create them as the regular pi user. How are you calling the startup scripts now? You said “I use supervisor to run about 6 startup scripts” … 2) DISPLAY might be used by the sound system anyway, headless or not. See if it works with sudo -i then DISPLAY=:0 aplay /home/pi/alerts/buzzer_alert.wav – scruss May 5 '18 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.