How to run tmux/screen with systemd > 230?

I run 16.04 and systemd now kills tmux when the user disconnects (summary of the change).

Is there a way to run tmux or screen (or any similar program) with systemd 230? I read all the heated disussion about pros and cons of the behavious but no solution was suggested.

(I see the behaviour in 229 as well)


Solution 1:

The proper solution is to disable the offending systemd behavior system-wide.

Edit /etc/systemd/logind.conf (you must sudo, of course) and set

KillUserProcesses=no

You can also put this setting in a separate file, e.g. /etc/systemd/logind.conf.d/99-dont-kill-user-processes.conf.

Then restart systemd-logind.service.

sudo systemctl restart systemd-logind

Solution 2:

Based on @Rinzwind's answer and inspired by a unit description the best I could find is to use TaaS (Tmux as a Service) - a generic detached instance of tmuxone reattaches to.

# cat /etc/systemd/system/[email protected]

[Unit]
Description=tmux default session (detached)
Documentation=man:tmux(1)

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/tmux new-session -d -s %I
ExecStop=/usr/bin/tmux kill-server
KillMode=none

[Install]
WantedBy=multiplexer.target

# systemctl start [email protected]
# systemctl start [email protected]
# tmux list-sessions

instanceone: 1 windows (created Sun Jul 24 00:52:15 2016) [193x49]
instancetwo: 1 windows (created Sun Jul 24 00:52:19 2016) [193x49]

# tmux attach-session -t instanceone

(instanceone)#

Solution 3:

RemainAfterExit=

Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.

Solution 4:

Using Ubuntu 18.04 with systemd version 237, none of the suggested solutions worked for me.

The solution that worked for me was to

  • edit the /etc/systemd/logind.conf
  • uncomment KillExlcudeUsers
  • add a space separated list of users (e.g., KillExlcudeUsers=root user1 user2)

Solution 5:

You need to set the Type of the service to forking, as explained here.

Let's assume the service you want to run in screen is called minecraft. Then you would open minecraft.service in a text editor and add or edit the entry Type=forking under the section [Service].