以用户身份运行的系统服务在注销时终止

jiw*_*ene 10 users systemd services logout

我有一个为选定用户执行的系统服务。名称是serv@<uid><uid>是用户的 UID)。我想与服务共享用户的资源限制(将其放入user-<uid>.slice),并且我还想使用他的权限。

/etc/systemd/system/serv@.service

[Unit]
Description=My service for user %i

[Service]
Type=simple
ExecStart=/usr/local/bin/serv.sh
Restart=always
User=%i
Slice=user-%i.slice
SuccessExitStatus=0 255 SIGKILL SIGTERM

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

当用户注销时,使用 SIGTERM 终止服务。为什么服务没有重新启动?如何设置才不会终止服务?

小智 13

对我来说,解决方案是loginctl enable-linger username。如果没有这个,当用户注销时服务就会终止。https://wiki.archlinux.org/index.php/Systemd/User#Automatic_start-up_of_systemd_user_instances


jiw*_*ene 6

您可以更改登录配置:

\n\n

/etc/systemd/logind.conf

\n\n
[Login]\n\xe2\x80\xa6\nKillUserProcesses=no\n\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n\n

这也会改变其他服务和后台进程的行为(例如screentmux)。参见问题#3483

\n\n
\n\n

在那之后跑systemctl restart systemd-logind

\n