-
Notifications
You must be signed in to change notification settings - Fork 164
Open
Labels
Description
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
pulseaudioin system wide mode (not user mode) - set
XDG_RUNTIME_DIRproperly 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
exit-idle-time = -1
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/