Systemd/User Services/zh: Difference between revisions
No edit summary |
Created page with "同样,您也可以为特定用户禁用某项服务:" Tags: Mobile edit Mobile web edit |
||
(3 intermediate revisions by the same user not shown) | |||
Line 58: | Line 58: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
同样,您也可以为特定用户禁用某项服务: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
Line 71: | Line 69: | ||
== 用法 == | == 用法 == | ||
要与用户相关的 systemd 服务交互,请在 <code>systemctl</code> 命令中使用 <code>--user</code> 标志。例如,要检查用户服务的状态: | |||
<syntaxhighlight lang="console"> $ systemctl --user status my-cool-user-service </syntaxhighlight> | <syntaxhighlight lang="console"> $ systemctl --user status my-cool-user-service </syntaxhighlight> | ||
要查看特定用户服务的日志,请使用 <code>journalctl</code> 和 <code>--user-unit</code> 选项: | |||
<syntaxhighlight lang="console"> $ journalctl --user-unit my-cool-user-service </syntaxhighlight> | <syntaxhighlight lang="console"> $ journalctl --user-unit my-cool-user-service </syntaxhighlight> | ||
列出所有活跃用户单位: | |||
<syntaxhighlight lang="console"> $ systemctl --user list-units </syntaxhighlight> | <syntaxhighlight lang="console"> $ systemctl --user list-units </syntaxhighlight> | ||
[[Category:systemd|分类:systmed]] | [[Category:systemd|分类:systmed]] |
Latest revision as of 15:09, 10 October 2025
Systemd supports running a separate instance of systemd for a given user, allowing the user to control their own services. See here for more information: https://wiki.archlinux.org/title/Systemd/User
In NixOS, a user service can be expressed with systemd.user.services.<name>
, as documented here: https://search.nixos.org/options?query=systemd.user.services
This may be useful if you want a user to be able to start, stop, and restart their own instance of a service without needing to make the user a sudoer.
Here is an example:
systemd.user.services.my-cool-user-service = {
enable = true;
after = [ "network.target" ];
wantedBy = [ "default.target" ];
description = "My Cool User Service";
serviceConfig = {
Type = "simple";
ExecStart = ''/my/cool/user/service'';
};
};
By default, user services will be stopped when the user logs out and will start again when the user logs back in due to us setting wantedBy = [ "default.target" ]
in the example.
注销后保持用户服务运行
If you need a user service to stay running after a user logs out, you need to enable "lingering" by setting users.users.<username>.linger = true;
You'll also likely want to change to wantedBy = [ "multi-user.target" ];
so the service starts at boot time.
为特定用户启用服务
By default, enabling a user service enables it for every user for which systemd spawns a service manager. If you wish for the service to be run only for specific users (say, UserA
and UserB
), use ConditionUser
(man 5 systemd.unit
):
systemd.user.services.my-cool-user-service = {
unitConfig.ConditionUser = "UserA|UserB";
};
同样,您也可以为特定用户禁用某项服务:
systemd.user.services.my-cool-user-service = {
unitConfig.ConditionUser = "!root";
};
用法
要与用户相关的 systemd 服务交互,请在 systemctl
命令中使用 --user
标志。例如,要检查用户服务的状态:
$ systemctl --user status my-cool-user-service
要查看特定用户服务的日志,请使用 journalctl
和 --user-unit
选项:
$ journalctl --user-unit my-cool-user-service
列出所有活跃用户单位:
$ systemctl --user list-units