systemd 用户模式下的服务非活动死机

systemd 用户模式下的服务非活动死机

目前使用 RHEL 7.9,我必须激活 systemctl 用户模式,创建一个名为[电子邮件受保护]这不是问题,但我认为按照本指南链接:https://serverfault.com/questions/936985/cannot-use-systemctl-user-due-to-failed-to-get-d-bus-connection-permission

我创建了一个测试服务:

[Unit]
Description=Foo

[Service]
ExecStart=/home/glcmsd/test.sh

[Install]
WantedBy=multi-user.target

哪里test.sh

#!/bin/bash
sleep 2h

然后我跑

systemctl daemon-reload --user
systectl start test --user
systemctl enable test --user

服务工作正常

-bash-4.2$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-08 10:14:38 BST; 5s ago
Main PID: 97445 (test.sh)
CGroup: /user.slice/user-1258074.slice/[email protected]/test.service
├─97445 /bin/bash /home/glcmsd/test.sh
└─97446 sleep 2h

然后运行 ​​sudo restart ,当我检查服务状态时,它已处于非活动状态

-bash-4.2$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: inactive (dead)

关于如何调试它或我做错了什么的任何线索?

编辑:我在 rhel 8.2 上做了相同的测试,它不需要遵循指南,我得到了相同的结果

EDIT2:谢谢您的回答,非常有帮助。尝试遵循 Torin 的解决方案,它适用于 RHEL 8.2,但不适用于 7.9 这是 8.2 中的实现:

$ cat /home/glcmsd/.config/systemd/user/test.service
[Unit]
Description=Foo

[Service]
ExecStart=/home/glcmsd/test.sh

[Install]
WantedBy=default.target

$ systemctl stop test --user
$ systemctl disable test --user
Removed /home/glcmsd/.config/systemd/user/multi-user.target.wants/test.service.
$ systemctl daemon-reload --user
$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; disabled; vendor preset: enabled)
Active: inactive (dead)

Apr 08 12:10:52 npc027161 systemd[2187]: Started Foo.
Apr 08 13:23:08 npc027161 systemd[2187]: Stopping Foo...
Apr 08 13:23:08 npc027161 systemd[2187]: Stopped Foo.
$ systemctl start test --user
$ systemctl enable test --user
Created symlink /home/glcmsd/.config/systemd/user/default.target.wants/test.service → /home/glcmsd/.config/systemd/user/test.service.
[glcmsd@npc027161 ~]$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-08 13:24:59 BST; 27s ago
Main PID: 16291 (test.sh)
CGroup: /user.slice/user-1258074.slice/[email protected]/test.service
├─16291 /bin/bash /home/glcmsd/test.sh
└─16292 sleep 2h

Apr 08 13:24:59 npc027161 systemd[2187]: Started Foo.
$ sudo reboot
#################################################################################
[glcmsd@npc027161 ~]$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-08 13:27:16 BST; 5s ago
Main PID: 1798 (test.sh)
CGroup: /user.slice/user-1258074.slice/[email protected]/test.service
├─1798 /bin/bash /home/glcmsd/test.sh
└─1801 sleep 2h

Apr 08 13:27:16 npc027161 systemd[1777]: Started Foo.
[glcmsd@npc027161 ~]$

这与 7.9 上的实现相同:

-bash-4.2$ cat /home/glcmsd/.config/systemd/user/test.service
[Unit]
Description=Foo
[Service]
ExecStart=/home/glcmsd/test.sh

[Install]
WantedBy=default.target
-bash-4.2$ vi /home/glcmsd/.config/systemd/user/test.service
-bash-4.2$ systemctl stop test --user
-bash-4.2$ systemctl disable test --user
Removed symlink /home/glcmsd/.config/systemd/user/multi-user.target.wants/test.service.
-bash-4.2$ systemctl daemon-reload --user
-bash-4.2$
-bash-4.2$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; disabled; vendor preset: enabled)
Active: inactive (dead)
-bash-4.2$ systemctl start test --user
-bash-4.2$ systemctl enable test --user
Created symlink from /home/glcmsd/.config/systemd/user/default.target.wants/test.service to /home/glcmsd/.config/systemd/user/test.service.
-bash-4.2$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-08 13:29:46 BST; 20s ago
Main PID: 3369 (test.sh)
CGroup: /user.slice/user-1258074.slice/[email protected]/test.service
├─3369 /bin/bash /home/glcmsd/test.sh
└─3370 sleep 2h
-bash-4.2$ sudo reboot
#################################################################################
-bash-4.2$ systemctl status test --user
● test.service - Foo
Loaded: loaded (/home/glcmsd/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: inactive (dead)

EDIT3:目前我放弃在 rhel7.9 上以用户模式使用 systemd。我注意到 systemd 版本之间存在一些差异。Rhel 7.9 有 systemd 219,8.2 有 239。systemd239 似乎通过创建 2 个服务来允许用户模式,一个用于在启动时启用延迟。另一个为用户创建运行时目录。我注意到 239 中还有一个运行时目录可执行文件,但在 219 上不存在。不知道现在该怎么做。如果上次编辑不准确,请原谅,稍后会更新它并添加更多信息

答案1

multi-user.target是一个系统目标,可能不是您想要的。

systemd.特殊(7)

特殊用户单位

当 systemd 作为用户实例运行时,以下特殊单元可用:

默认目标
这是用户会话的主要目标,默认启动。组成正常用户会话的各种服务应该被拉入此目标。在这方面,default.target 与系统实例中的 multi-user.target 类似,但它是一个真实的单元,而不是别名。

此外,以下单位的定义与系统对应单位类似:exit.target、shutdown.target、sockets.target、timers.target、paths.target、bluetooth.target、printer.target、smartcard.target、sound 。目标。

所以你会想要切换到WantedBy=default.target

相关内容