加载systemd服务时访问显示

加载systemd服务时访问显示

我正在用 C 和 D 语言编写可启动服务。

有一个具有瘦客户端设置的用户数据库。我在下载过程中通过网络访问该数据库,并从那里获取链接到 MAC 地址的特定用户的数据。事实上,用户可以将两个或多个显示器连接到瘦客户端。每个监视器的数据也从数据库加载。我使用xrandr源代码编写了一个程序,在知道显示器 ID 的情况下,将所需的显示器设置在一个位置或另一个位置。事实上,它xrandr使用了全局变量DISPLAY,该变量在加载过程中不可用。

● myservice-init.service - myservice
     Loaded: loaded (/etc/systemd/system/myservice-init.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2022-05-12 19:15:14 MSK; 55s ago
    Process: 1559 ExecStart=/etc/init.d/myservice-init (code=exited, status=1/FAILURE)
   Main PID: 1559 (code=exited, status=1/FAILURE)

мая 12 19:15:14 ts_d85ed3156a39 systemd[1]: Starting myservice...
мая 12 19:15:14 ts_d85ed3156a39 thinstation[1562]: Can't open display :0
мая 12 19:15:14 ts_d85ed3156a39 systemd[1]: myservice-init.service: Main process exited, code=exited, status=1/FAILURE
мая 12 19:15:14 ts_d85ed3156a39 systemd[1]: myservice-init.service: Failed with result 'exit-code'.
мая 12 19:15:14 ts_d85ed3156a39 systemd[1]: Failed to start myservice.

不过,看看Xorg服务 - 这些数据就在那里:

~# cat /etc/X11/xinit/xinitrc.d/50-systemd-user.sh

#!/bin/sh

systemctl --user import-environment DISPLAY XAUTHORITY

if command -v dbus-update-activation-environment >/dev/null 2>&1; then
    dbus-update-activation-environment DISPLAY XAUTHORITY
fi

下载后我开始服务xorg.service。但即使将此代码添加到我的服务的启动中,我仍然收到错误:

● myservice-init.service - myservice
     Loaded: loaded (/etc/systemd/system/myservice-init.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2022-05-12 19:08:52 MSK; 18s ago
    Process: 1560 ExecStart=/etc/init.d/myservice-init (code=exited, status=1/FAILURE)
   Main PID: 1560 (code=exited, status=1/FAILURE)

May 12 19:08:52 ts_d85ed3156a39 systemd[1]: Starting myservice...
May 12 19:08:52 ts_d85ed3156a39 thinstation[1564]: dbus-update-activation-environment: error: unable to connect to D-Bus: Unable to autolaun
May 12 19:08:52 ts_d85ed3156a39 thinstation[1570]: Can't open display :0
May 12 19:08:52 ts_d85ed3156a39 systemd[1]: myservice-init.service: Main process exited, code=exited, status=1/FAILURE
May 12 19:08:52 ts_d85ed3156a39 systemd[1]: myservice-init.service: Failed with result 'exit-code'.
May 12 19:08:52 ts_d85ed3156a39 systemd[1]: Failed to start myservice.

如果我只是启动我的应用程序,那么它的运行结果非常正确:

IFace("enp3s0", "e8:5b:d2:11:6a:39", "192.168.0.15")
[Monitor("VGA-1", true, 1280, 1024)]

UPD。

创建了用户登录时启动的用户服务,但DISPLAY也不可用:

● myservice-init.service - myservice
     Loaded: loaded (/etc/systemd/user/myservice-init.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2022-05-12 22:31:30 MSK; 8min ago
    Process: 2550 ExecStart=/etc/init.d/myservice-init (code=exited, status=1/FAILURE)
   Main PID: 2550 (code=exited, status=1/FAILURE)

мая 12 22:31:30 ts_d85ed3156a39 systemd[2546]: Started myservice-init.
мая 12 22:31:30 ts_d85ed3156a39 myservice-init[2551]: Can't open display :0.0
мая 12 22:31:30 ts_d85ed3156a39 systemd[2546]: myservice-init.service: Main process exited, code=exited, status=1/FAILURE
мая 12 22:31:30 ts_d85ed3156a39 systemd[2546]: myservice-init.service: Failed with result 'exit-code'.

相关内容