我正在运行 Linux Mint 内核版本 4.15.0-42-generic,并且有一个用户 systemd 服务文件~/.local/share/systemd/user/my.service
:
[Unit]
Description=My service
After=network.target
After=systemd-user-sessions.service
After=network-online.target
StartLimitBurst=5
StartLimitIntervalSec=30s
[Service]
ExecStart="/my/command"
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=5
PrivateTmp=true
Environment=DISPLAY=:0
[Install]
WantedBy=default.target
它的安装方法是放置该文件,然后运行systemctl --user daemon-reload
,然后systemctl --user enable my.service
.
它工作得很好,除非操作系统崩溃并启动硬重启(电源循环)。重新登录后,该服务未运行。
事实上,它甚至没有被发现。
重新启动后的第一个命令证明 systemd 已丢失服务,即使该文件仍然存在并且没有更改:
matt@brego:~$ systemctl --user status my.service
Unit my.service could not be found.
matt@brego:~$ ls -la ~/.local/share/systemd/user
total 20
drwxr-xr-x 2 matt matt 4096 Oct 16 15:56 .
drwxr-xr-x 3 matt matt 4096 Sep 28 13:58 ..
-rw-r--r-- 1 matt matt 458 Oct 16 15:56 my.service
matt@brego:~$ loginctl list-sessions
SESSION UID USER SEAT TTY
c2 1000 matt seat0
1 sessions listed.
运行systemctl --user start my.service
或systemctl --user enable my.service
会导致相同的错误:Unit my.service could not be found.
我必须运行systemctl --user daemon-reload
然后才能启用该服务。每一个。时间。后。难的。重新启动。
如何强制 systemd 在用户登录、系统重新引导和电源循环后运行该服务?并且不要忘记服务?
编辑以解决进一步的问题
以同一用户身份运行的硬重启后各种命令的输出:
matt@brego:~$ cat /etc/pam.d/systemd-user
# This file is part of systemd.
#
# Used by systemd --user instances.
@include common-account
session required pam_selinux.so close
session required pam_selinux.so nottys open
session required pam_loginuid.so
session required pam_limits.so
@include common-session-noninteractive
session optional pam_systemd.so
matt@brego:~$ ps -U ${UID} -f | grep systemd
matt 1341 1 0 13:34 ? 00:00:00 /lib/systemd/systemd --user
matt 1384 1341 0 13:34 ? 00:00:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
matt 6870 1913 0 14:12 pts/1 00:00:00 grep --color=auto systemd
matt@brego:~$ systemctl --user list-units --type=target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
default.target loaded active active Default
paths.target loaded active active Paths
sockets.target loaded active active Sockets
timers.target loaded active active Timers
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
5 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
matt@brego:~$ systemctl --user status default.target
● default.target - Default
Loaded: loaded (/usr/lib/systemd/user/default.target; static; vendor preset: enabled)
Active: active since Tue 2018-12-04 13:34:34 MST; 38min ago
Docs: man:systemd.special(7)
Dec 04 13:34:34 brego systemd[1341]: Reached target Default.