Linux Mastering Development

Play a sound from a daemon Linux

I have Manjaro Linux and I am trying to play a sound (mp3 and wav) using a script from a daemon.
Basically, I have a with a command trying to play the sound. This script is called from a daemon. (Below more details over the code).

Quick notes:

  • The script run successfully from my command line (Without daemon)
  • I know my script use root as user, but I need the sound in my session, for that, I just add a "prefix" before the main command to execute the command as user myself
  • As a single command for play sound (Without daemon and without prefix) my both options works (I tried with paplay and cvlc)
  • With the "Prefix" only works cvlc
  • This is the prefix that I have used sudo -u julian DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/1000/bus" ***Code to play sound***
  • The codes to play sound are: paplay lowBattery2.wav --volume 65536 and cvlc --play-and-exit lowBattery.mp3
  • Both codes with prefix works correctly BUT when exists another sound is playing (For example youtube) the command line returns an error.


My problem:

  • I can NOT play a sound from a daemon parallelly with another sound (For example youtube)

My goal:

  • Execute this daemon to turn on my notifications when the battery of my pc is low.

And that’s it.

This is the

     battery=$(cat /sys/class/power_supply/BAT0/capacity)

sayHelloMessage="Hi ${user}!"
messagePart1="Your computer has "
messagePart2="% of battery, please connect it"

while true; do
        if [ "$batteryLimit" -gt "$battery" ]; then
                echo $finalMessage;
                echo $user
                echo $userID
                sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${userID}/bus" notify-send "${sayHelloMessage}" "${finalMessage}"
                ##sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/${userID}/bus"  cvlc --play-and-exit  lowBattery.mp3;
                sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/${userID}/bus" paplay  lowBattery2.wav --volume 65536
                ##ffplay -autoexit lowBattery.mp3;
                ##paplay lowBattery2.wav --volume 65536
                echo "Sound ended"

                echo "Your battery is greater than ${batteryLimit+1} you are ok!";
        echo $(date);
        sleep 10;

And this one is the daemon:

Description=To show notifications when battery is smaller than 18%


ExecStart= sh /etc/systemd/system/LowBatteryScripts/





The logs of the errors (With "prefix") are:
for cvlc:

[000055d038a38c10] alsa audio output error: cannot open ALSA device "default": Dispositivo o recurso ocupado
[000055d038a38c10] main audio output error: Falló salida de audio
[000055d038a38c10] main audio output error: El dispositivo de audio «default» no se puede usar:
Dispositivo o recurso ocupado
[000055d038a38c10] main audio output error: module not functional
[00007f1164c1fb50] main decoder error: failed to create audio output

for paplay:
Connection failed: Connection denied

pa_context_connect () failed: Connection denied

Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *