systemd / systemd

The systemd System and Service Manager
https://systemd.io
GNU General Public License v2.0
13.1k stars 3.75k forks source link

User level tmux service automatically disabled #12981

Open aiguofer opened 5 years ago

aiguofer commented 5 years ago

systemd version the issue has been seen with

systemd/disco-updates,now 240-6ubuntu5.1 amd64 [installed]

Used distribution

Ubuntu 19.04

Unexpected behaviour you saw

I have a user level service which keeps getting disabled when I reboot. I can enable it and make sure the symlinks are correctly created, but when I reboot it's not enabled anymore (and the symlink is gone). I should note that this is only happening with the tmux service; I have a lot of other services that are working perfectly.

Steps to reproduce the problem

  1. Create service file ~/.local/share/systemd/user/tmux.service:
    
    [Unit]
    Description=Start my tmux session

[Service] RemainAfterExit=yes ExecStart=/usr/bin/tmux start-server ExecStop=/usr/bin/tmux kill-server

[Install] WantedBy=started.target

2. Enable it:
```shell
$ systemctl --user enable tmux
Created symlink /home/aiguofer/.config/systemd/user/started.target.wants/tmux.service → /home/aiguofer/.local/share/systemd/user/tmux.service.
  1. Make sure the symlink is indeed created:
    $ ll ~/.config/systemd/user/started.target.wants/tmux.service
    lrwxrwxrwx 1 aiguofer aiguofer 53 Jul  7 22:47 /home/aiguofer/.config/systemd/user/started.target.wants/tmux.service -> /home/aiguofer/.local/share/systemd/user/tmux.service
  2. Reboot
  3. Notice that tmux.service is disabled (although sometimes it does start):
    
    $ systemctl --user status tmux.service
    ● tmux.service - Start my tmux session
    Loaded: loaded (/home/aiguofer/.local/share/systemd/user/tmux.service; disabled; vendor preset: enabled)
    Active: active (exited) since Sun 2019-07-07 22:58:32 MDT; 1min 43s ago
    Process: 4606 ExecStart=/usr/bin/tmux start-server (code=exited, status=0/SUCCESS)
    Main PID: 4606 (code=exited, status=0/SUCCESS)
    CGroup: /user.slice/user-1000.slice/user@1000.service/tmux.service
           ├─ 4626 /usr/bin/tmux start-server
           ├─ 5813 -zsh
           ├─ 5883 -zsh
           ├─ 5936 -zsh
           ├─ 6015 -zsh
           ├─ 6119 -zsh
           ├─ 6190 -zsh
           ├─19066 systemctl --user status tmux.service
           └─19067 less

Jul 07 22:58:32 X1-Carbon-Diego systemd[2385]: Started Start my tmux session.

and
```shell
$ ll ~/.config/systemd/user/started.target.wants/tmux.service
ls: cannot access '/home/aiguofer/.config/systemd/user/started.target.wants/tmux.service': No such file or directory
poettering commented 5 years ago

uh, this is weird... have you checked if there's anything in the logs of someone removing that symlink? systemd should not do anything like that...

aiguofer commented 5 years ago

yeah I tried grepping journalctl for anything related to tmux and can't seem to find anything in there.

I'm currently working around the issue by changing the script that starts started.target to:

# hack to work around https://github.com/systemd/systemd/issues/12981
systemctl --user enable tmux

dbus-update-activation-environment --systemd PATH XDG_CURRENT_DESKTOP XDG_DATA_DIRS XDG_MENU_PREFIX XDG_RUNTIME_DIR XDG_SEAT XDG_SESSION_DESKTOP XDG_SESSION_ID XDG_SESSION_TYPE XDG_VTNR SSH_AUTH_SOCK DBUS_SESSION_BUS_ADDRESS QT_QPA_PLATFORMTHEME

systemctl --user start started.timer

I'm on a single user system so it'd have to be "something" rather than "someone".

poettering commented 5 years ago

do you have any of those XDG path env vars set?

aiguofer commented 5 years ago

@poettering yeah, XDG_CURRENT_DESKTOP, XDG_DATA_DIRS, XDG_RUNTIME_DIR, XDG_SEAT, XDG_SESSION_DESKTOP, XDG_SESSION_ID, XDG_SESSION_TYPE, XDG_VTNR are all set.

I start most of my session processes using systemd and I kept running into issues where some programs weren't getting the ENV vars they needed. I started just copying my entire environment and my problems went away. At some point, I started having problems copying the entire environment (I believe due to malformed variables) so I took a list of everything that seemed important and dumped it there. I'm not sure I need all of those, but I never felt like 'optimizing'.

That being said, I wouldn't imagine that would cause that 1 service to auto-deactivate.

Something I did notice after adding my hack to enable the service before starting. It seems like it gets disabled right after starting. Now, every time I reboot:

$ systemctl --user status tmux.service
● tmux.service - Start my tmux session
   Loaded: loaded (/home/aiguofer/.local/share/systemd/user/tmux.service; disabled; vendor preset: enabled)
   Active: active (exited) since Thu 2019-07-11 17:56:33 MDT; 27min ago
  Process: 5155 ExecStart=/usr/bin/tmux start-server (code=exited, status=0/SUCCESS)
 Main PID: 5155 (code=exited, status=0/SUCCESS)
   CGroup: /user.slice/user-1000.slice/user@1000.service/tmux.service

So it does indeed get enabled by my hack, it does get started when started.target starts, but then it shows that the service is disabled.

iredelmeier commented 4 years ago

I can confirm the same behaviour, also with Ubuntu 19.04 and systemd v240.

Enabling or disabling linger also has no effect.

My XDG variables are set as follows:

XDG_RUNTIME_DIR=/run/user/1000
XDG_SEAT=seat0
XDG_SESSION_CLASS=user
XDG_SESSION_ID=1
XDG_SESSION_TYPE=tty
XDG_VTNR=1