Debian – Systemd Service stops on its user exit – debian 9 stretch

debianservicessystemdteamspeak

So I'm sitting on a problem I faced while setting up my teamspeak server (properly!). The Teamspeak Server and it's config is totally fine. My Problem started when I wanted it to start on system-startup.
Now for how to create a autostart script etc. I have already done it, and it has worked in the past. But this time I want to do it with a systemd service only, since there already is a startscript provided by teamspeak.

So now after giving some context let's get to the problem:
With the service configuration (see below), which I am using right now, the teamspeak server starts perfectly on system-startup. But when I login with user "teamspeak" do some stuff and then type exit, the teamspeak server is shutting down and restarted right after that.

[Unit]
Description=TeamSpeak 3 Server

[Service]
ExecStart=/teamspeak/ts3server_startscript.sh start
ExecStop=/teamspeak/ts3server_startscript.sh stop
PIDFile=/teamspeak/ts3server.pid
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=teamspeak_server
User=teamspeak
Group=teamspeak
Type=Forking
[Install]
WantedBy=multi-user.target

So the restart comes from "Restart=always" and I am hundred percent sure, that the teamspeak-shutdown comes from the service. I already tried "RemainAfterExit=true" but the only thing that did, is not making the teamspeak server restart after the user-logout (since it let's it think the service is still active, but the processes where all closed. At least that is how I understand the service is working and why it is stopping my teamspeak server.

So in short: How do I prevent the service from calling the ExecStop Command when the teamspeak user exits his session (in my case putty, ssh-connection)

Here is a good summary of all systemd.service options: click me

I, even though it plays against what I wrote above, also wrote a script that wraps the ts3server_minimal_startscript.sh into a screen session. The script worked perfectly fine when used manually, but I still had exactly the same problem using it via the service. As I said, I would prefer a non-screen version since the ts3server_startscript.sh already creates a own process for the ts-server.

EDIT:
I forgot to say, that I created the service at /etc/systemd/system/teamspeak.service.
As of a request, here is the systemctl status answer before I exit teamspeak user:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sat 2018-04-14 17:35:08 CEST; 23h a
go
  Process: 3344 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 3361 (ts3server)
    Tasks: 17 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─3361 ./ts3server

Apr 14 17:35:08 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 14 17:35:08 srv83030 teamspeak_server[3357]: Starting the TeamSpeak 3 
server

1ms after I exited teamspeak user:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 1ms a
go
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 5388 (ts3server_start)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5388 /bin/sh /teamspeak/ts3server_startscript.sh start

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.

2s after I exited Teamspeak user:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 2s ag
o
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 5392 (ts3server)
    Tasks: 16 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5392 ./ts3server

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 15 16:55:11 srv83030 teamspeak_server[5388]: Starting the TeamSpeak 3 
server

And one more just after I logged back in with teamspeak user:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 3min 43s ago
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited, status=0/SUCCESS)
 Main PID: 5392 (ts3server)
    Tasks: 16 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5392 ./ts3server

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 15 16:55:11 srv83030 teamspeak_server[5388]: Starting the TeamSpeak 3 
server

EDIT2: Perhaps it is also useful to know, that the home directory of teamspeak user is at /teamspeak . So exactly there where also the startscript is.

Best Answer

You might try configuring systemd to not kill user processes on logout for the wanted user:

sudo loginctl enable-linger teamspeak