Pactl invoked from systemd service always reports "pa_context_connect () failed connection failed"

I have set up a systemd service file to do some pactl operations on system startup for a test process. Although the commands work fine when executed from the terminal, I always get the "pa_context_connect () failed connection failed" message when running the same script from the systemd service, starting the service. I also use the "User =" directive in the service file to ensure that the auto login user is the same as the user used to run the service commands.

I read that it has something to do with the fact that the pulseaudio session is not valid in the context of the harmlessness of the systemd service, but I was unable to figure it out further.

+2


source to share


1 answer


While it might be a little late for any project you might have been working on, here's what I found out.

The regular systemctl, PID 1, really cannot access the environement variables of the current user when the service starts. Since pactl relies on these variables to find which instance of pulseaudio to connect to, it cannot do so when the service starts. I'm sure there is a rather dirty workaround for this, but I found something better.



Most systems have a second instance of systemd running in user space (accessible through systemctl --user

, not connected as root). This instance can indeed access all of the environment variables and I found that pactl does not return any errors when called directly or through a script.

All you have to do is put your services in /usr/lib/systemd/user/

, /etc/systemd/user/

or ~/.config/systemd/user/

remove the directive User=

from your service file and run systemctl --user daemon-reload

as a regular user to make sure they 'were discovered.

+3


source







All Articles