3

I have a service which uses both the network and sound(ALSA), and I'm trying to make a systemd system service out of it. It works as a user service, but I can't make it work as a system service.

dbus-run-session[16623]: ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect:
dbus-run-session[16623]: Alsa error PCM open ALSA function 'snd_pcm_open' failed with error 'ECONNREFUSED: Connection refused'
dbus-run-session[16623]: Could not start audio: Alsa error: PCM open failed

Here's what I have so far. It's a template unit, the parameter is my user name. Commented out the sandboxing settings.

# /etc/systemd/system/[email protected]
[Unit]
Description=A spotify playing daemon
Documentation=https://github.com/Spotifyd/spotifyd
Wants=sound.target
After=sound.target
Wants=network-online.target
After=network-online.target

[Service]
User=%i

Type=exec
ExecStart=/usr/bin/dbus-run-session /usr/local/bin/spotifyd --no-daemon

Restart=always
RestartSec=12

ProtectSystem=full
#ProtectSystem=strict

#ProtectHome=tmpfs
#TemporaryFileSystem=/home/%i
#BindPaths=/home/%i/.config/pulse
#BindReadOnlyPaths=/home/%i/.local/share/

#ProtectHome=read-only
#ReadWritePaths=/home/%i/.config/pulse
#ReadOnlyPaths=/home/%i/.local/share/

#PrivateTmp=true
#NoNewPrivileges=true

[Install]
WantedBy=default.target

Comments:

  • The daemon is not compiled with pulseaudio support, only ALSA. Not sure if that would matter.
  • dbus-run-session is needed otherwise it fails at startup, since it register a d-bus name.
  • I prefer it as a system service instead of a user one because I can depend on the network-online
  • I'm running the service with my user, but could be a new user as well, as long as I have d-bus/MPRIS working
  • I don't want to run PulseAudio/ALSA as a system service
  • Group=audio didn't work

So what's wrong with the audio setup? I expected a system service switched to a regular account to have the same outcome as a user service.

2
  • Is that user in the pulse-access group?
    – CL.
    Commented May 17, 2020 at 12:26
  • @CL. no, it isn't. Adding it to the Group/SupplementaryGroups didn't help either. Commented May 17, 2020 at 17:10

1 Answer 1

3

I expected a system service switched to a regular account to have the same outcome as a user service.

It can be, although one thing is not shared: the environment.

This was required to successfully access ALSA. (Source: https://askubuntu.com/questions/70560/why-am-i-getting-this-connection-to-pulseaudio-failed-error)

Environment=XDG_RUNTIME_DIR=/run/user/1000

Which makes me realize I should just run as an user service. The lifetime of the system service and the user session don't match.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .