26

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)

muru
  • 197,895
  • 55
  • 485
  • 740
WoJ
  • 1,315
  • 6
  • 19
  • 34
  • 1
    That's odd - I have systemd-229-4ubuntu7 (from xenial-updates) and I don't see this behaviour. Anyway, this behaviour has already been reverted in Debian's systemd-230-2, so it'll probably be reverted in Ubuntu soon too. – MvanGeest Jul 24 '16 at 15:41
  • https://www.mirbsd.org/~tg/Debs/dists/jessie/wtf/Pkgs/mirabilos-support/ has prevent-systemd-* packages that allow you to install and use sysvinit as earlier – mirabilos May 10 '17 at 20:59

6 Answers6

21

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
  • 5
    How is the proper solution to a problem specific to one unit to set some system wide setting? – Julia Path Aug 13 '17 at 18:37
  • Because, as you can see from even only reading the question, let alone all of the other discussion elsewhere about this that the question indirectly references, this is not specific to one application. This is a point that is even in boldface in the question. – JdeBP Aug 17 '17 at 09:04
  • 2
    You just need to enable lingering for the user via loginctl enable-linger <user>. From man loginctl: "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." – Adrian Günter May 09 '18 at 02:02
  • 2
    Ubuntu 18.04 here. This does not work, not even after reboot. Enabling lingering does not either. – Pa_ Feb 13 '19 at 12:29
15

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/tmux@.service

[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 tmux@instanceone.service
# systemctl start tmux@instancetwo.service
# 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)#
WoJ
  • 1,315
  • 6
  • 19
  • 34
  • This is neat but I can't for the life of me figure out why you'd use it! :) – sarnold Dec 09 '16 at 05:43
  • 9
    @sarnold: to be able to control applications which start with a console, but which are suppose to be running at boot time. Minecraft is one example. – WoJ Dec 09 '16 at 11:59
7

RemainAfterExit=

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

Rinzwind
  • 299,756
  • Thanks. I accepted and +1 your answer as it provides the core of the solution. Would you know if it is usable in an interactive mode (by launching tmux interactively, as opposed to my answer)? – WoJ Jul 24 '16 at 06:59
  • doubt it. systemd probably will not care about your tmux unless you tell systemd it should :-P (their approach to this is rather single-minded.... it seems like it is their way or no way.) You need a unit description like you posted. Feel free to accept yours and/or expand yours ;-) I dont need the rep >:-D – Rinzwind Jul 24 '16 at 07:06
  • 3
    This answer needs more detail such as where to use that setting. – kasperd Jul 24 '16 at 08:50
  • 1
    Sure but the answer is going to be a copy/paste of the manual page ;-) – Rinzwind Jul 24 '16 at 11:00
  • 1
    @WoJ You can make an alias for tmux to start as systemd-run --remain-after-exit tmux (or maybe even with --scope --user flags). – Debilski Aug 02 '16 at 20:30
4

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].

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)
fear
  • 266
4

According to https://unix.stackexchange.com/a/287282/117599 invoking tmux using

systemd-run --user --scope tmux

should also do the trick.

phk
  • 391
  • 1
  • 3
  • 15