Yes, there is a solution.
We cannot play sound in systemd service + no login normally.
this is because pulseaudio starts when user log in.
when you login to some user, the pulseaudio user service starts and /run/user/{u-id}/pulse will be created.
but without login, /run/user/{u-id}/pulse will not created, so sound_play cannot play the sound.
i found a solution and there are 2 things to do in the solution.
- run
pulseaudio in system wide mode (not user mode)
- set
XDG_RUNTIME_DIR properly in your service
run pulseaudio in system wide mode (not user mode)
disable user pulseaudio service
sudo systemctl --global disable pulseaudio.service pulseaudio.socket
enable system pulseaudio service
put the following pulseaudio.service file in /etc/systemd/system
[Unit]
Description=PulseAudio system server
[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio --daemonize=no --system --realtime --disallow-exit --disallow-module-loading --log-target=journal
Restart=on-failure
[Install]
WantedBy=multi-user.target
change /etc/pulse/default.pa and /etc/pulse/system.pa
set this in default.pa and system.pa
load-module module-native-protocol-unix auth-anonymous=1
change /etc/pulse/client.conf
set this in /etc/pulse/client.conf
default-server = /var/run/pulse/native
autospawn = no
change /etc/pulse/daemon.conf
set this in /etc/pulse/daemon.conf
add all users in audio, bluetooth pulse-access groups
sudo adduser {user name} audio
sudo adduser {user name} bluetooth
sudo adduser {user name} pulse-access
set XDG_RUNTIME_DIR properly in your service
set XDC_RUNTIME_DIR in your service file
[Service]
Environment=XDG_RUNTIME_DIR=/var/run
Reboot
Then you can now play sound from systemd
ref. https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/SystemWide/
Yes, there is a solution.
We cannot play sound in systemd service + no login normally.
this is because
pulseaudiostarts when user log in.when you login to some user, the
pulseaudiouser service starts and/run/user/{u-id}/pulsewill be created.but without login,
/run/user/{u-id}/pulsewill not created, sosound_playcannot play the sound.i found a solution and there are 2 things to do in the solution.
pulseaudioin system wide mode (not user mode)XDG_RUNTIME_DIRproperly in your servicerun
pulseaudioin system wide mode (not user mode)disable user
pulseaudioserviceenable system
pulseaudioserviceput the following
pulseaudio.servicefile in/etc/systemd/systemchange
/etc/pulse/default.paand/etc/pulse/system.paset this in
default.paandsystem.pachange
/etc/pulse/client.confset this in
/etc/pulse/client.confchange
/etc/pulse/daemon.confset this in
/etc/pulse/daemon.confadd all users in
audio,bluetoothpulse-accessgroupsset
XDG_RUNTIME_DIRproperly in your serviceset
XDC_RUNTIME_DIRin your service fileReboot
Then you can now play sound from
systemdref. https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/SystemWide/