我的 systemd 服务无法运行

我的 systemd 服务无法运行

我试图在启动时在笔记本电脑上设置 xinput 设置,因此我使用所有设置和基本的 systemd 服务文件创建了一个脚本:

[Unit]
Description=Sets my preferred xinput settings

[Service]
ExecStart=/usr/local/bin/SCRIPTS/xinput.sh

[Install]
WantedBy=multi-user.target

我跑了systemctl enable xinput-settings.service,但这不起作用,systemctl start但运行sudo bash /usr/local/bin/SCRIPTS/xinput.sh(脚本需要 root)也不起作用,但这个 systemd 服务不会。我尝试将用户设置为 root 以及我看到的一些其他修复,但没有任何修复它。

这是脚本内容:

xinput --set-prop "TPPS/2 IBM TrackPoint" 320 -0.4
exit 0

我使用的是 Arch Linux 内核 5.6.12-arch1-1

systemctl status xinput-settings说:

● xinput-settings.service - Sets my preferred xinput settings
     Loaded: loaded (/etc/systemd/system/xinput-settings.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Fri 2020-05-15 23:34:35 +04; 42ms ago
    Process: 16809 ExecStart=/usr/local/bin/SCRIPTS/xinput.sh (code=exited, status=203/EXEC)
   Main PID: 16809 (code=exited, status=203/EXEC)

May 15 23:34:35 ThinkX250 systemd[1]: Started Sets my preferred xinput settings.
May 15 23:34:35 ThinkX250 systemd[16809]: xinput-settings.service: Failed to execute command: No such file or directory
May 15 23:34:35 ThinkX250 systemd[16809]: xinput-settings.service: Failed at step EXEC spawning /usr/local/bin/SCRIPTS/xinput.sh: No such file or directory
May 15 23:34:35 ThinkX250 systemd[1]: xinput-settings.service: Main process exited, code=exited, status=203/EXEC
May 15 23:34:35 ThinkX250 systemd[1]: xinput-settings.service: Failed with result 'exit-code'.

答案1

请考虑这个服务单元:

[Unit]
Description=Sets my preferred xinput settings

[Service]
Type=oneshot
ExecStart=/usr/local/bin/SCRIPTS/xinput.sh
Environment="DISPLAY=:0"
RemainAfterExit=yes

[Install]
WantedBy=graphical.target

逐行解释:

  • Type=oneshot适合执行一次就结束的脚本。如果您不指定此项,则Type=simple默认为 systemd,并且 systemd 可能会对您的服务如此迅速地结束感到惊讶。我想这可能是你的第一个原因Active: failed
  • Environment="DISPLAY=:0"。您可以让多个用户登录并同时激活多个显示器。 systemd 未连接到桌面/显示器,因此如果它运行 xinput,xinput 将失败。通过设置此环境变量,您可以告诉 xinput 使用哪个显示
  • RemainAfterExit=yes。不是绝对必要的,但我喜欢这样的oneshot类型。这意味着当服务完成时,其状态将active (exited)变为inactive (dead)
  • WantedBy=graphical.target意思是这个服务会在xserver启动后启动。

如果仍然有问题,可能与 xauthority 有关。在这种情况下,请在这里查看一个很好的答案:

在 systemd 服务文件中设置 DISPLAY

答案2

将我的脚本添加到 KDE 自动启动即使使用 sudo 也能工作。

相关内容