I have noticed that a logoff (log out) from my X user session will kill any tmux session I have initiated, even sessions I had run with sudo tmux and similar commands. I am sure that this formerly did not happen, but some recent change has effected this behavior.
How do I maintain these tmux (or screen) sessions, even after I end my X session?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This “feature” has existed in systemd previously, but the systemd developers decided to effect a change in the default, to enable the setting for termination of child processes upon log out of a session.
You can revert this setting in your logind.conf (/etc/systemd/logind.conf):
KillUserProcesses=no
You can also run tmux with a systemd-run wrapper like the following:
systemd-run --scope --user tmux
For these systems, you may just want to alias the tmux (or screen) command:
alias tmux="systemd-run --scope --user tmux"
Method 2
The missing bit can be found on SU:
First: Keep the $USER‘s SystemD instance running after logoff:
sudo loginctl enable-linger $USER
Second: Reboot. (According to man loginctl this becomes effective on the next boot only.)
Third: Start tmux under control of the $USER‘s SystemD instance:
systemd-run --scope --user tmux
That’s it.
There is no need to change /etc/systemd/logind.conf for this.
Note:
- This is needed only for Users who are not
root. - If you start
tmuxdirectly (not viasystemd-run), it get’s killed. - Hence either use an alias or a wrapper script like follows:
#!/bin/bash [ -x /usr/bin/systemd-run ] && dbus-send --print-reply / org.freedesktop.DBus.Peer.Ping && /usr/bin/systemd-run --scope --user -- /bin/true && exec /usr/bin/systemd-run --scope --user -- /usr/bin/tmux "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0f490">[email protected]</a>" exec /usr/bin/tmux "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="795d39">[email protected]</a>"
I install this with chmod +x "$HOME/bin/tmux" to avoid edits of .bashrc which might vanish if your profile is reset.
Update: /usr/bin/systemd-run --scope --user -- /bin/true is needed to test that systemd-run is usable in ssh context while X11 is active in parallel. Here, to run permanent tmux, you apparently need to run it from X11 session, not ssh.
Update 2: Reboot is needed and linger state can be found with
ls -al /var/lib/systemd/linger
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0