Systemd – Fix Per-User Instance Not Starting Service Unit Automatically

debiansystemd

I want the systemd per-user instance to start a following service unit at bootup:

user.name@svr:~$ systemctl --user cat tmux
# /etc/systemd/user/tmux.service
[Unit]
Description=Start tmux in detached session

[Service]
Type=forking
ExecStart=/usr/bin/tmux new-session -s test -d
ExecStop=/usr/bin/tmux kill-session -t test
Restart=always
RestartSec=1

[Install]
WantedBy=default.target
user.name@svr:~$

Lingering is enabled:

user.name@svr:~$ sudo loginctl show-user user.name | grep Linger=
Linger=yes
user.name@svr:~$

However, when I restart the server, then service is not started:

user.name@svr:~$ systemctl --user status tmux
● tmux.service - Start tmux in detached session
   Loaded: loaded (/etc/systemd/user/tmux.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
user.name@svr:~$

I can start the tmux service manually with systemctl --user status tmux without any issues. Also, according to systemctl --user status the systemd per-user instance itself is running:

user.name@svr:~$ systemctl --user status 
● svr
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Tue 2019-12-03 22:02:41 UTC; 15min ago
   CGroup: /user.slice/user-1000.slice/user@1000.service
           └─init.scope
             ├─456 /lib/systemd/systemd --user
             └─462 (sd-pam)
user.name@svr:~$ 

This should mean that there are no issues with Linux PAM configuration.

Why doesn't systemd per-user instance start a service unit automatically?

Best Answer

I enabled(systemctl --user enable tmux) the tmux.service at the time when I had WantedBy=multi-user.target in the [Install] section of the /etc/systemd/user/tmux.service and later forgot to re-enable the service. Checking the content of the ~/.config/systemd/user/ directory helped me to spot my mistake.

Related Question