85

I'm trying to create a systemd user unit and have it start when the system boots. The service does start manually, however it does not start at boot.

From searching the Internet I learned that in order for user units to start at boot, I supposedly need to run loginctl enable-linger <username>, but this seems to have had no effect at all. Indeed, the man page says:

       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.

Despite running this command, my service doesn't start at boot.

loginctl enable-linger error

The unit:

$ 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

When I reboot, it is not running:

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

# systemctl status user@1000 -l
● [email protected] - User Manager for UID 1000
   Loaded: loaded (/usr/lib/systemd/system/[email protected]; 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/[email protected]
           ├─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.

I can start it manually, and it works:

$ systemctl --user start thin@redmine
$ systemctl --user status thin@redmine -l
● [email protected] - A fast and very simple Ruby web server
   Loaded: loaded (/home/error/.config/systemd/user/[email protected]; 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/[email protected]/thin.slice/[email protected]
           └─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
● [email protected] - User Manager for UID 1000
   Loaded: loaded (/usr/lib/systemd/system/[email protected]; 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/[email protected]
           ├─613 /usr/lib/systemd/systemd --user
           ├─615 (sd-pam)                                                       
           └─thin.slice
             └─[email protected]
               └─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"

How do I figure out why this service doesn't start at boot, and get it to do so?

The operating system is Fedora 23 x86_64, systemd 222-10.

1
  • 4
    Is error a common user name? I had to read twice to understand what loginctl enable-linger error was intending. Commented Nov 5, 2020 at 20:01

1 Answer 1

122

Figures that I'd figure this out on my own.

The clue was here, in the user service output:

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

My unit was asking to be loaded with multi-user.target, but there is no such target in the user systemd.

I changed this to default.target in the unit file, disabled and re-enabled the service, and it now starts at boot time.

[Install]
WantedBy=default.target

and then

$ systemctl --user disable thin@redmine
Removed symlink /home/error/.config/systemd/user/multi-user.target.wants/[email protected].
$ systemctl --user daemon-reload
$ systemctl --user enable thin@redmine
Created symlink from /home/error/.config/systemd/user/default.target.wants/[email protected] to /home/error/.config/systemd/user/[email protected].
3
  • 2
    Great. Thanks! Did also lean on multi-user.target for my user unit...
    – gue
    Commented Feb 25, 2016 at 7:54
  • 21
    Thank you very much! Use systemctl --user list-units --type=target to list the targets for the user systemd.
    – phinz
    Commented Nov 22, 2018 at 19:22
  • 16
    Also, be sure to run loginctl enable-linger USERNAME, otherwise the services with will be started on user login instead of boot. I know OP mentioned it in the question, but it took me a while to figure it out, so I'm posting it here too in case it helps anyone.
    – user000001
    Commented Mar 13, 2021 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .