Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we play sound in systemd service + no login? #219

Open
knorth55 opened this issue Feb 7, 2023 · 0 comments
Open

Can we play sound in systemd service + no login? #219

knorth55 opened this issue Feb 7, 2023 · 0 comments
Labels

Comments

@knorth55
Copy link
Member

knorth55 commented Feb 7, 2023

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.

  1. run pulseaudio in system wide mode (not user mode)
  2. 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

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/

@knorth55 knorth55 changed the title Cannot play sound in systemd service + no login Can we play sound in systemd service + no login? Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant