为什么我的systemd用户单元不能在启动时启动?


39

我正在尝试创建一个systemd用户单元,并在系统启动时启动它。该服务会手动启动,但是不会在启动时启动。

通过搜索Internet,我了解到要使用户单元在启动时启动,我应该运行loginctl enable-linger <username>,但这似乎没有任何作用。实际上,手册页中说:

       Enable/disable user lingering for one or more users. If enabled for
       a specific user, a user manager is spawned for the user at boot and
       kept around after logouts. This allows users who are not logged in
       to run long-running services.

尽管运行此命令,但我的服务在启动时无法启动。

loginctl enable-linger error

那个单位:

$ cat ~/.config/systemd/user/thin\@.service
[Unit]
Description=A fast and very simple Ruby web server

[Service]
Type=simple
EnvironmentFile=/home/error/.config/thin/%i
ExecStart=/usr/bin/bash /home/error/.rvm/wrappers/%i/thin start -a $THIN_BIND -p $THIN_PORT
WorkingDirectory=/srv/www/%i
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新启动后,它没有运行:

$ systemctl --user status thin@redmine -l
● thin@redmine.service - A fast and very simple Ruby web server
   Loaded: loaded (/home/error/.config/systemd/user/thin@.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

# systemctl status user@1000 -l
● user@1000.service - User Manager for UID 1000
   Loaded: loaded (/usr/lib/systemd/system/user@.service; static; vendor preset: disabled)
   Active: active (running) since Wed 2015-12-23 19:43:27 GMT; 13s ago
 Main PID: 613 (systemd)
   Status: "Startup finished in 38ms."
   CGroup: /user.slice/user-1000.slice/user@1000.service
           ├─613 /usr/lib/systemd/systemd --user
           └─615 (sd-pam)                                                          

Dec 23 19:43:27 redmine systemd[613]: Reached target Sockets.
Dec 23 19:43:27 redmine systemd[613]: Starting Sockets.
Dec 23 19:43:27 redmine systemd[613]: Reached target Timers.
Dec 23 19:43:27 redmine systemd[613]: Starting Timers.
Dec 23 19:43:27 redmine systemd[613]: Reached target Basic System.
Dec 23 19:43:27 redmine systemd[613]: Starting Basic System.
Dec 23 19:43:27 redmine systemd[613]: Reached target Default.
Dec 23 19:43:27 redmine systemd[613]: Startup finished in 38ms.
Dec 23 19:43:27 redmine systemd[613]: Starting Default.
Dec 23 19:43:27 redmine systemd[1]: Started User Manager for UID 1000.

我可以手动启动它,并且可以正常工作:

$ systemctl --user start thin@redmine
$ systemctl --user status thin@redmine -l
● thin@redmine.service - A fast and very simple Ruby web server
   Loaded: loaded (/home/error/.config/systemd/user/thin@.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2015-12-23 19:43:50 GMT; 5s ago
 Main PID: 1265 (ruby)
   CGroup: /user.slice/user-1000.slice/user@1000.service/thin.slice/thin@redmine.service
           └─1265 ruby /home/error/.rvm/gems/ruby-2.2.1@redmine/bin/thin start -a ::1 -p 8008

Dec 23 19:43:50 redmine systemd[613]: Started A fast and very simple Ruby web server.
Dec 23 19:43:50 redmine systemd[613]: Starting A fast and very simple Ruby web server...
Dec 23 19:43:52 redmine bash[1265]: /home/error/.rvm/gems/ruby-2.2.1@redmine/gems/htmlentities-4.3.1/lib/htmlentities/mappings/expanded.rb:465: warning: duplicated key at line 466 ignored: "inodot"

# systemctl status user@1000 -l
● user@1000.service - User Manager for UID 1000
   Loaded: loaded (/usr/lib/systemd/system/user@.service; static; vendor preset: disabled)
   Active: active (running) since Wed 2015-12-23 19:43:27 GMT; 40s ago
 Main PID: 613 (systemd)
   Status: "Startup finished in 38ms."
   CGroup: /user.slice/user-1000.slice/user@1000.service
           ├─613 /usr/lib/systemd/systemd --user
           ├─615 (sd-pam)                                                       
           └─thin.slice
             └─thin@redmine.service
               └─1265 ruby /home/error/.rvm/gems/ruby-2.2.1@redmine/bin/thin start -a ::1 -p 8008                                                               

Dec 23 19:43:27 redmine systemd[613]: Startup finished in 38ms.
Dec 23 19:43:27 redmine systemd[613]: Starting Default.
Dec 23 19:43:27 redmine systemd[1]: Started User Manager for UID 1000.
Dec 23 19:43:50 redmine systemd[613]: Created slice -.slice.
Dec 23 19:43:50 redmine systemd[613]: Starting -.slice.
Dec 23 19:43:50 redmine systemd[613]: Created slice thin.slice.
Dec 23 19:43:50 redmine systemd[613]: Starting thin.slice.
Dec 23 19:43:50 redmine systemd[613]: Started A fast and very simple Ruby web server.
Dec 23 19:43:50 redmine systemd[613]: Starting A fast and very simple Ruby web server...
Dec 23 19:43:52 redmine bash[1265]: /home/error/.rvm/gems/ruby-2.2.1@redmine/gems/htmlentities-4.3.1/lib/htmlentities/mappings/expanded.rb:465: warning: duplicated key at line 466 ignored: "inodot"

我如何弄清楚为什么该服务无法在启动时启动,并使其启动?

操作系统是Fedora 23 x86_64,系统版本222-10。

Answers:


60

我自己想出来的数字。

线索在这里,在用户服务输出中:

Dec 23 19:43:27 redmine systemd[613]: Reached target Default.

我的单元要求加载multi-user.target,但用户systemd中没有这样的目标。

我将其更改为default.target在单位文件中,禁用并重新启用了该服务,它现在在启动时启动。

[Install]
WantedBy=default.target

接着

$ systemctl --user disable thin@redmine
Removed symlink /home/error/.config/systemd/user/multi-user.target.wants/thin@redmine.service.
$ systemctl --user daemon-reload
$ systemctl --user enable thin@redmine
Created symlink from /home/error/.config/systemd/user/default.target.wants/thin@redmine.service to /home/error/.config/systemd/user/thin@.service.

2
大。谢谢!难道还依靠multi-user.target为我的用户单元...
GUE

2
非常感谢你!使用systemctl --user list-units --type=target列出的目标用户systemd。
菲茨
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.