启用的 systemd 用户服务不会在登录时启动

Wou*_*345 10 systemd

我希望用户服务nfancurve.service在每次登录时开始运行。我之前使用命令启用了该服务systemctl --user enable nfancurve.service,但是,每次我启动新会话(包括重新启动)时,该服务都没有运行,尽管它已启用,我在登录后通过运行这些命令进行验证:

\n
name@machine:~$ systemctl --user is-active nfancurve.service\ninactive\nname@machine:~$ systemctl --user is-enabled nfancurve.service\nenabled\nname@machine:~$ systemctl --user start nfancurve.service\nname@machine:~$ systemctl --user is-active nfancurve.service\nactive\n
Run Code Online (Sandbox Code Playgroud)\n

这是来自以下位置的状态日志systemctl --user status nfancurve.service

\n
\xe2\x97\x8f nfancurve.service - Nfancurve service\n     Loaded: loaded (/usr/lib/systemd/user/nfancurve.service; enabled; vendor preset: enabled)\n     Active: inactive (dead)\n
Run Code Online (Sandbox Code Playgroud)\n

有关完整信息,该服务来自此存储库,我从 AUR 安装了该存储库。该.service文件如下所示:

\n
[Unit]\nDescription=Nfancurve service\nAfter=graphical-session.target\nRequires=graphical-session.target\n\n[Service]\nExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf\nKillSignal=SIGINT\n\n[Install]\nWantedBy=graphical-session.target\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试将最后一行更改为WantedBy=default.target,但重新启动并登录后服务仍然没有启动。我的桌面环境是 xfce,以防考虑到graphical-session.target.

\n

如何确保它在每次登录时自动启动?

\n

Ste*_*art 10

太长了;博士

\n

改成。WantedBy=graphical-session.targetWantedBy=default.target

\n

假设

\n

我怀疑您的问题出在[Install]您的服务文件部分。对于系统单元来说,基于安装它multi-user.target可能很正常,但对于用户单元来说,等效的安装是default.target。有关详细信息,请参阅systemd.special(7) 联机帮助页

\n
| system target     | near-equivalent user target |\n|-------------------|-----------------------------|\n| multi-user.target | default.target              |\n| graphical.target  | graphical-session.target    |\n
Run Code Online (Sandbox Code Playgroud)\n

您可能也感兴趣的其他目标是graphical-session-pre.targetxdg-desktop-autostart.target

\n

默认.目标 MVCE

\n

这是我刚刚在 Debian 11 机器上尝试过的一个最小示例:

\n

服务文件:

\n
$ systemctl --user cat simpleuser.service\n# /home/stew/.config/systemd/user/simpleuser.service\n[Unit]\nDescription=Simple User Service\n\n[Service]\nType=oneshot\nRemainAfterExit=yes\nExecStart=/bin/true\n\n[Install]\nWantedBy=default.target\n
Run Code Online (Sandbox Code Playgroud)\n

我安装了它:

\n
$ systemctl --user enable simpleuser.service\nCreated symlink /home/stew/.config/systemd/user/default.target.wants/simpleuser.service \xe2\x86\x92 /home/stew/.config/systemd/user/simpleuser.service.\n
Run Code Online (Sandbox Code Playgroud)\n

我重新启动后可以看到该服务处于活动状态:

\n
$ systemctl --user status simpleuser.service\n\xe2\x97\x8f simpleuser.service - Simple User Service\n     Loaded: loaded (/home/stew/.config/systemd/user/simpleuser.service; enabled; vendor preset: enable>\n     Active: active (exited) since Sun 2020-07-12 10:12:54 CEST; 13min left\n    Process: 1127 ExecStart=/bin/true (code=exited, status=0/SUCCESS)\n   Main PID: 1127 (code=exited, status=0/SUCCESS)\n\nJul 12 10:12:54 stewbian systemd[1106]: Starting Simple User Service...\nJul 12 10:12:54 stewbian systemd[1106]: Finished Simple User Service.\n
Run Code Online (Sandbox Code Playgroud)\n

如果您想知道子(exited)状态:如果您使用类似 和Type=simple的内容ExecStart=/bin/sleep infinity,那么您会得到(running)

\n

图形会话目标问题

\n

我在 github 上以错误报告的形式找到了你的问题。

\n

如果您基于graphical-session.target进行安装,则可能无法启动。我刚刚在我的 KDE/i3 设置上运行了这个,并在普通的 Gnome3 设置上重现。

\n
$ systemctl --user status graphical-session.target\n\xe2\x97\x8f graphical-session.target - Current graphical user session\n     Loaded: loaded (/usr/lib/systemd/user/graphical-session.target; static; vendor preset: disabled)\n     Active: inactive (dead)\n       Docs: man:systemd.special(7)\n
Run Code Online (Sandbox Code Playgroud)\n

听起来 DE 并没有真正按照其设计实现这个目标。在这种情况下,使用WantedBy=default.target. 你的脚本看起来不是基于 GUI 的,所以我打赌default.target应该没问题。否则,您可以考虑After=graphical.target在您的[Unit]部分中使用。

\n

根据systemd.special(7) 手册页graphical-session.target应该是BoundBy=DE(即{gnome,kde,xfce}-session.target)。这样,当 Gnome 启动时,它会引发graphical-session.target。当我检查 dbus 对象时,我清楚地看到与任何会引发它的目标没有任何关系。

\n
$ busctl introspect --user org.freedesktop.systemd1 \\\n    /org/freedesktop/systemd1/unit/graphical_2dsession_2etarget \\\n    org.freedesktop.systemd1.Unit\nNAME                             TYPE      SIGNATURE RESULT/VALUE                             FLAGS\n.ActiveState                     property  s         "inactive"                               emits-change\n.After                           property  as        2 "simpleuser.service" "basic.target"    const\n.Before                          property  as        1 "shutdown.target"                      const\n.BindsTo                         property  as        0                                        const\n.BoundBy                         property  as        0                                        const\n.Conflicts                       property  as        1 "shutdown.target"                      const\n.ConsistsOf                      property  as        2 "gnome-terminal-server.service" "duns\xe2\x80\xa6 const\n.Description                     property  s         "Current graphical user session"         const\n.Documentation                   property  as        1 "man:systemd.special(7)"               const\n.LoadState                       property  s         "loaded"                                 const\n.Names                           property  as        1 "graphical-session.target"             const\n.PartOf                          property  as        0                                        const\n.RequiredBy                      property  as        0                                        const\n.Requires                        property  as        1 "basic.target"                         const\n.SubState                        property  s         "dead"                                   emits-change\n.UnitFilePreset                  property  s         "disabled"                               -\n.UnitFileState                   property  s         "static"                                 -\n.WantedBy                        property  as        0                                        const\n.Wants                           property  as        1 "simpleuser.service"                   const\n\n
Run Code Online (Sandbox Code Playgroud)\n

我深入研究了邮件列表,发现{gnome,kde,xfce}-session.target四年前才提出这个建议,目的是让 gnome、kde、xfce 发送这些*.target文件。它还没有发生。因此,即使有记录,它也是不完整的。因此您还不应该使用graphical-session.target

\n


Wou*_*345 2

我终于找到了解决方案(感谢斯图尔特和其他人)。我更改了原来的服务文件:

[Unit]
Description=Nfancurve service
After=graphical-session.target
Requires=graphical-session.target

[Service]
ExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf
KillSignal=SIGINT

[Install]
WantedBy=graphical-session.target
Run Code Online (Sandbox Code Playgroud)

通过删除 Requires 行并替换graphical-session.targetby的所有实例default.target(包括AfterWantedBy行)。这导致我当前的服务文件:

[Unit]
Description=Nfancurve service
After=default.target

[Service]
ExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf
KillSignal=SIGINT

[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)

现在,当我启动计算机并登录时,该服务处于活动状态。