从systemd服务调用的pactl始终报告"pa_context_connect()失败的连接被拒绝". [英] pactl called from systemd service always reports "pa_context_connect() failed connection refused"

查看:1675
本文介绍了从systemd服务调用的pactl始终报告"pa_context_connect()失败的连接被拒绝".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了systemd服务文件,以在系统启动时执行一些pactl操作,以进行测试过程.从终端执行命令时,这些命令运行正常,但通过启动服务从systemd服务运行相同脚本时,我总是会收到"pa_context_connect()连接失败拒绝"的信息.我还在服务文件中使用"User ="指令,以确保自动登录用户与用于运行服务命令的用户匹配.

I've setup a systemd service file to perform some pactl operations at system startup for a test process. While the commands work fine when performed from a terminal I always get "pa_context_connect() failed connection refused" when running the same script from the systemd service by starting the service. I'm also using the 'User=' directive in the service file to ensure that the auto-login user matches the user used to run the service commands.

我已经了解到,这与Pulseaudio会话在systemd服务的无环境上下文中无效有关,但是我无法进一步弄清这一点.

I've read that this is somehow related to the pulseaudio session not being valid in the environmentless context of the systemd service but I haven't been able to figure that out further.

推荐答案

尽管对于您可能正在从事的任何项目来说可能都有些晚,但这是我发现的.

Although it might be a bit late for whatever project you might have been be working on, here's what I found out.

启动服务时,常规systemctl PID 1确实无法访问当前用户的环境变量.由于pactl依靠这些变量来查找需要连接到哪个pulseaudio实例,因此在通过服务启动时无法做到这一点.我敢肯定有一个相当肮脏的解决方法,但是我发现了更好的方法.

The regular systemctl, the PID 1, indeed cannot access the environement variables of the current user when launching a service. Since pactl relies on those variables to find what instance of pulseaudio it needs to connect to, it is unable to do so when launched though a service. I'm sure there's a fairly dirty workaround for this, but I found something better.

大多数系统都有在用户空间中运行的systemd的第二个实例(可通过systemctl --user访问,但未作为root用户连接).这个实例确实可以访问所有用户空间环境变量,并且我发现pactl在直接调用或通过脚本调用时不会返回任何错误.

Most systems have a second instance of systemd running in userspace (accessible through systemctl --user while not connected as root). This instance indeed can access all the userspace environment variables and I found that pactl doesn't return any errors when being called either directly or through a script.

所有您需要做的就是将服务放在/usr/lib/systemd/user//etc/systemd/user/~/.config/systemd/user/中,从服务文件中删除User=指令并以常规用户身份运行systemctl --user daemon-reload以确保它们已被检测到.

All you need to do is put your services in either /usr/lib/systemd/user/, /etc/systemd/user/, or ~/.config/systemd/user/, remove the User= directive from your service file and run systemctl --user daemon-reload as a regular user to make sure they've been detected.

这篇关于从systemd服务调用的pactl始终报告"pa_context_connect()失败的连接被拒绝".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆