Skip to content

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

Description

@knorth55

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/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions