Systemd/User Services/ru: Difference between revisions
|  Created page with "В NixOS пользовательский сервис может быть выражен с помощью {{ic|systemd.user.services.<name>}}, как описано здесь: https://search.nixos.org/options?query=systemd.user.services." |  Updating to match new version of source page | ||
| (9 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| {{Systemd/breadcrumb}} | |||
| Systemd  | |||
| Systemd поддерживает запуск отдельного экземпляра systemd для конкретного пользователя, позволяя ему управлять своими собственными службами. | |||
| В NixOS пользовательский сервис может быть выражен с помощью {{ic|systemd.user.services.<name>}}, как описано здесь: https://search.nixos.org/options?query=systemd.user.services. | В NixOS пользовательский сервис может быть выражен с помощью {{ic|systemd.user.services.<name>}}, как описано здесь: https://search.nixos.org/options?query=systemd.user.services. | ||
| Это может быть полезно, если вы хотите, чтобы пользователь мог запускать, останавливать и перезапускать свой собственный экземпляр службы без необходимости делать его sudoer. | |||
| Пример сервиса: | |||
| <syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| systemd.user.services.my-cool-user-service = { | systemd.user.services.my-cool-user-service = { | ||
| Line 14: | Line 14: | ||
|    after = [ "network.target" ]; |    after = [ "network.target" ]; | ||
|    wantedBy = [ "default.target" ]; |    wantedBy = [ "default.target" ]; | ||
|    description = " |    description = "My Cool User Service"; | ||
|    serviceConfig = { |    serviceConfig = { | ||
|        Type = "simple"; |        Type = "simple"; | ||
| Line 21: | Line 21: | ||
| }; | }; | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
| По умолчанию пользовательские сервисы будут остановлены, когда пользователь выйдет из системы, и запустятся снова, когда пользователь снова войдет в систему, благодаря тому, что мы установили {{ic|<nowiki>wantedBy = ["default.target" ]</nowiki>}} в примере. | |||
| <span id="Keeping_user_services_running_after_logout"></span> | |||
| == Продолжение работы пользовательских служб после выхода из системы == | |||
| Если вам нужно, чтобы пользовательский сервис продолжал работать после выхода пользователя из системы, вам нужно включить "[https://search.nixos.org/options?channel=unstable&show=users.users.%3Cname%3E.linger&from=0&size=50&sort=relevance&type=packages&query=users.users.%3Cname%3E.linger lingering]", установив {{ic|<nowiki>users.users.<username>.linger = true;</nowiki>}}. | |||
| Вы также, вероятно, захотите изменить {{ic|<nowiki>wantedBy = ["multi-user.target" ];</nowiki>}}, чтобы служба запускалась во время загрузки. | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | |||
| == Enabling a service for specific users == | |||
| </div> | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | |||
| 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, {{ic|<nowiki>UserA</nowiki>}} and {{ic|<nowiki>UserB</nowiki>}}), use {{ic|<nowiki>ConditionUser</nowiki>}} ({{ic|<nowiki>man 5 systemd.unit</nowiki>}}): | |||
| </div> | |||
| <syntaxhighlight lang="nix"> | |||
| systemd.user.services.my-cool-user-service = { | |||
|   unitConfig.ConditionUser = "UserA|UserB"; | |||
| }; | |||
| </syntaxhighlight> | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Likewise, you can also disable a service for a specific user: | |||
| </div> | </div> | ||
| <syntaxhighlight lang="nix"> | |||
| systemd.user.services.my-cool-user-service = { | |||
|   unitConfig.ConditionUser = "!root"; | |||
| }; | |||
| </syntaxhighlight> | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| ==  | == Usage == | ||
| </div> | </div> | ||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| To interact with user-specific systemd services, use the <code>--user</code> flag with the <code>systemctl</code> command. For example, to check the status of a user service: | |||
| </div> | </div> | ||
| <syntaxhighlight lang="console"> $ systemctl --user status my-cool-user-service </syntaxhighlight> | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| To view logs for a specific user service, use <code>journalctl</code> with the <code>--user-unit</code> option: | |||
| </div> | </div> | ||
| <syntaxhighlight lang="console"> $ journalctl --user-unit my-cool-user-service </syntaxhighlight> | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | |||
| To list all active user units: | |||
| </div> | |||
| <syntaxhighlight lang="console"> $ systemctl --user list-units </syntaxhighlight>  | |||
| <div lang="en" dir="ltr" class="mw-content-ltr"> | |||
| [[Category:systemd]] | [[Category:systemd]] | ||
| </div> | |||
Latest revision as of 22:41, 7 October 2025
Systemd поддерживает запуск отдельного экземпляра systemd для конкретного пользователя, позволяя ему управлять своими собственными службами.
В NixOS пользовательский сервис может быть выражен с помощью systemd.user.services.<name>, как описано здесь: https://search.nixos.org/options?query=systemd.user.services.
Это может быть полезно, если вы хотите, чтобы пользователь мог запускать, останавливать и перезапускать свой собственный экземпляр службы без необходимости делать его sudoer.
Пример сервиса:
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'';
  };
};
По умолчанию пользовательские сервисы будут остановлены, когда пользователь выйдет из системы, и запустятся снова, когда пользователь снова войдет в систему, благодаря тому, что мы установили wantedBy = ["default.target" ] в примере.
Продолжение работы пользовательских служб после выхода из системы
Если вам нужно, чтобы пользовательский сервис продолжал работать после выхода пользователя из системы, вам нужно включить "lingering", установив users.users.<username>.linger = true;.
Вы также, вероятно, захотите изменить wantedBy = ["multi-user.target" ];, чтобы служба запускалась во время загрузки.
Enabling a service for specific users
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";
};
Likewise, you can also disable a service for a specific user:
systemd.user.services.my-cool-user-service = {
  unitConfig.ConditionUser = "!root";
};
Usage
To interact with user-specific systemd services, use the --user flag with the systemctl command. For example, to check the status of a user service:
 $ systemctl --user status my-cool-user-service
To view logs for a specific user service, use journalctl with the --user-unit option:
 $ journalctl --user-unit my-cool-user-service
To list all active user units:
 $ systemctl --user list-units
