1

I have a python app called main.py, that runs upon startup using rc.local. During this app's runtime, it runs an external bash script 'play.sh' that runs vlc to play a song. The code in the 'play.sh' is simple: /usr/bin/cvlc '/song.mp3'

The main.py code works great, it gets run on startup and all it's functionalities are intact, except running the play.sh file. When the main.py gets to the section in the code where it's supposed to run the 'play.sh', nothing happens.

The odd thing is, when I run the /etc/rc.local from the pi user when logged in, the 'play.sh' works as expected and the song is played. When running sudo /etc/rc.local , the issue persists. Even when running su - pi /etc/rc.local , the issue still persists.

I tried replacing vlc for mpv, but still no luck. What am I missing? What could be the reason for this?

Any help would be greatly appreciated.

P.S. this may be a bigger deal than I take it to be, but the Raspberry is connected via bluetooth to a speaker, and the audio is supposed to be routed there. It has no problem connected automatically to the speaker, and is connected when the issue occurs... but perhaps there is something there as well..

EDIT: I further investigated the issue. Upon closer inspection, it appears that it's something to do with the user environment. When I run a command 'su -m pi -c ' it works, but just calling su - pi -c '' doesn't work. How can I run with the env of the pi user upon startup? Either with rc.local, or systemd.

3
  • You need to have the graphical desktop running. rc.local runs too early for that. Add the program in the desktop startup file and it will run after you login.
    – Dougie
    May 16, 2023 at 6:43
  • Systemd is the same issue, I tried now. I edited the original question with more information as well. And as for Dougie, it appears the graphical desktop isn't related to anything, and rc.local runs just fine, part for the sound. I wish for the software to run without me having to log in. May 16, 2023 at 10:34
  • You might want to use systemd to start your script so that you can see the output and hopefully get some debug information. Take a look at the following blog for help in getting started with blog.usedbytes.com/2019/11/run-at-startup-without-rc.local
    – ukBaz
    May 16, 2023 at 10:46

1 Answer 1

1

I solved the issue. These prefixes in the bash script that plays the file fixed the issue. This file can now be run as sudo, and works perfectly.

#!/bin/bash
# Set the necessary environment variables
export XDG_RUNTIME_DIR="/run/user/$(id -u pi)"
export PULSE_RUNTIME_PATH="/run/user/$(id -u pi)/pulse"
export DISPLAY=:0

# Run VLC as the 'pi' user
su -m pi-c "cvlc /home/pi/Desktop/Music/song.mp3"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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