如何以 root 身份运行依赖于 xdg-desktop-autostart.target 的服务?

如何以 root 身份运行依赖于 xdg-desktop-autostart.target 的服务?

是否可以在依赖于或 的ExecStart情况下以 root 权限在 systemd 服务中运行该命令?如果是这样,怎么办?xdg-desktop-autostart.targetgraphical-session.target

在某些情况下,我尝试在 SteamOS 上将 Sunshine 游戏流媒体服务器作为服务运行。 SteamOS的游戏模式使用Wayland。为了让 Sunshine 能够与 Wayland 配合使用,我必须以 root 权限运行以下命令:

PULSE_SERVER=unix:$(pactl info | awk '/Server String/{print$3}') flatpak run dev.lizardbyte.sunshine

我已将该命令放入位于 的 bash 脚本中/home/deck/.local/bin/sunshine。我的systemd文件(sunshine.service)如下:

[Unit]
Description=Sunshine is a self-hosted game stream host for Moonlight.
StartLimitIntervalSec=500
StartLimitBurst=5
PartOf=graphical-session.target
Wants=xdg-desktop-autostart.target
After=xdg-desktop-autostart.target

[Service]
ExecStart=/home/deck/.local/bin/sunshine
ExecStop=flatpak kill dev.lizardbyte.sunshine
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=xdg-desktop-autostart.target

当开始放置/etc/systemd/service/并使用 启动时,此服务按预期工作sudo systemctl start sunshine.service。但是,尝试使用 启用该服务时会失败sudo systemctl enable sunshine.service。当启用该服务作为系统服务时,会抛出以下错误:

Unit /etc/systemd/system/sunshine.service is added as a dependency to a non-existent unit xdg-desktop-autostart.target.

我明白为什么会这样。该系统不像用户那样与图形会话绑定。但是,虽然当我将服务文件放入 中时,我可以将其启用为用户服务~/.config/systemd/user/,但它不适用于 Wayland,因为相关命令不是以 root 权限运行的。

相关内容