其他分享
首页 > 其他分享> > Systemd – 查找要使用的.Xauthority文件

Systemd – 查找要使用的.Xauthority文件

作者:互联网

我目前正在使用systemd单元文件来配置使用X服务器显示的服务.

X服务器实例由登录的用户(当前是pi用户)启动,但该服务是在root用户启动的.

如果我将.Xauthority文件位置硬编码到单元文件中的XAUTHORITY变量,我可以使用systemctl start test_graphic_app成功启动服务,如下所示

[Unit]
Description=Test Graphic App
After=multi-user.target

[Service]
Type=simple

User=root
Group=root

Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"

ExecStart=/usr/bin/python3 /usr/sbin/test_graphic_app.py

KillSignal=SIGINT
SuccessExitStatus=SIGINT

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=test_graphic_app

Restart=on-failure

[Install]
WantedBy=default.target

但是,如果我使用其他用户登录或者如果我在笔记本电脑上本地运行它,这显然不起作用,因为用户启动X不是pi

我想动态获取系统上的.Xauthority文件位置.

我尝试过使用sudo xauth info | grep权限| awk'{print $3}’如下

Environment="XAUTHORITY=$(/usr/bin/xauth info | grep Authority | awk '{print $3}')"

ExecStartPre=/bin/bash -c 'export XAUTHORITY=${XAUTHORITY}'

但是,如果该命令在我的笔记本电脑上运行,则它不在pi上

## On laptop ##
$sudo xauth info | grep "Authority file" | awk '{print $3}'
/run/user/1000/gdm/Xauthority

## On pi ##
$sudo xauth info | grep "Authority file" | awk '{print $3}'
xauth:  file /root/.Xauthority does not exist
/root/.Xauthority

我无法找到如何获取.Xauthority文件位置,具体取决于已启动X服务器实例的用户.另外,我不想让任何用户使用X显示做xhost

如何在我的系统单元中获取位置?

除了找到.Xauthority位置之外,还有更好的解决方案吗?

解决方法:

有更复杂的xhost版本,即xhost si:localuser:root,它只将本地用户root添加到允许的连接列表中.

您需要找到放置此命令的位置,以便在登录时运行,具体取决于您的分发.在/ etc / X11 /中查找已使用xhost的现有文件.在我的pi上,我在/etc/X11/Xsession.d/35×11-common_xhost-local中找到了它:

if type xhost >/dev/null 2>&1; then
  xhost +si:localuser:$(id -un) || :
fi

在另一个系统上,它位于/etc/X11/xinit/xinitrc.d/localuser.sh中.

标签:python,raspberry-pi,systemd,x-server,xauth
来源: https://codeday.me/bug/20190816/1670893.html