5

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.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Apahdos
  • 51
  • 1
  • 3
  • You also need to tell answerers where this service unit file is, and exactly how you enabled it. – JdeBP Apr 14 '18 at 09:05
  • Please post what `systemctl status teamspeak.service` (or whatever is the name of the service) looks like, preferrably before the issue and also right after the issue (after you log off from user "teamserver" from the SSH connection.) That should help understand what might be going on there. – filbranden Apr 15 '18 at 04:02
  • @JdeBP added now, thanks for reminding. Sorry that I haven't thought of that right away. – Apahdos Apr 15 '18 at 15:13
  • @FilipeBrandenburger added now, I hope this is helpful. Thanks for reminding though. – Apahdos Apr 15 '18 at 15:14
  • @Apahdos I still don't know what is going on... The `Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop` suggests you're correct that the service is being stopped using that ExecStop command. Perhaps look at the output of `sudo journalctl` around 2018-04-15 16:55:11 and see if it has any other clues... By any chance, is there a logout script in the home of teamserver user, such as `/teamserver/.bash_logout` or `/teamserver/.logout`? If so, any commands there that could be triggering something like that? – filbranden Apr 15 '18 at 16:29
  • Ok, so after some testing, I confirmed that the `ExecStop` command will be executed even if the main process (in your case `./ts3server`) is killed or terminated by some other agent. And, in your case, if `ts3server_startscript.sh stop` does nothing and exits with a zero status if the server is already down, you should see exactly this effect (`(code=exited, status=0/SUCCESS)` on the `ExecStop` process.) So I'm really thinking that it's *something else* killing/terminating `./ts3server` when you log out, and systemd is only restarting it. – filbranden Apr 16 '18 at 06:41
  • You can try to confirm that by logging in as `teamserver` and then just using the `kill` command on the PID of `t3server`, which you can see from the systemctl status output... Then you can confirm if that matches what you're seeing elsewhere. – filbranden Apr 16 '18 at 06:42
  • Not completely related to the issue at hand... But I noticed that you're using `Type=Forking` (instead of the correct `Type=forking`, lowercase.) I don't think that's related to your issue, but it means systemd is trying to use `Type=simple` instead (see output of `systemctl show -p Type teamspeak.service` to confirm that.) It seems everything still works... Just I noticed that `ExecStart` output is missing from `systemctl status` and that can be explained by it... You should fix that. – filbranden Apr 16 '18 at 06:44
  • Actually... The best approach would be if you *could* run this as a `Type=simple` service. That would simply require that you run `ts3server` in foreground instead. If you could modify `ts3server_startscript.sh` to just execute `t3server` in a way that it's blocking... And preferrably use the `exec` statement so that it replaces the shell script. That would be ideal. That's also assuming that to stop `ts3server`, sending a signal (SIGTERM) is enough, and there's no special procedure (though even then, `ExecStop` can help.) – filbranden Apr 16 '18 at 06:47
  • @FilipeBrandenburger First of all thank you so much for all your answers!. I checked the .bash_logout file, but there's only the clear console command. There is a minimal startscript for teamspeak, which does not start in another process and I will definitly try this solution in the next couple of days. I'm sorry for my late answer, I was not able to try stuff the past week. I never worked with exec before. A quick google said, it starts the command in a subprocess? But would that not result in the same problem? Also teamspeak start's as "simple" process. Thank you very much for that note. – Apahdos Apr 20 '18 at 20:02
  • Hey @Apahdos, happy to help! I hope we get to the bottom of what's causing this... The idea of Type=simple is that systemd will run something on foreground and the service will stop when that thing exits, terminates... You use "exec" in a shell script to replace the script with that other command, so in that sense it runs under the same PID. This is so once your script is done, systemd will keep monitoring the main pid of what you actually want to run. When you use Type=simple service, usually you don't need any ExecStop= commands, since then systemd will just kill it when you stop it. – filbranden Apr 20 '18 at 22:40
  • 1
    Hi @FilipeBrandenburger. Sorry for beeing inactive. I haven't tried much for solving the problem anymore, since what we discussed here didn't help and I did not have the time to stick my head behind such a small error. But the strange thing: I changed the host of the server. So now I have a virtual server instead of a dedicated one. Even though I installed the same OS the problem solved itself. Probably it has to do with some of the host's configs. But now relogging does not restart the server. It's not realy solved, but the at least the problem does not occur anymore. Thank you for helping! – Apahdos Nov 17 '18 at 09:42

2 Answers2

6

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

sudo loginctl enable-linger teamspeak
meuh
  • 49,672
  • 2
  • 52
  • 114
  • 1
    `loginctl enable-linger` is for processes that are started within the session of the user, which is not the case here since this is started as a service on the machine (in other words, outside the user's session.) I haven't tested this, but I wouldn't expect this would work... – filbranden Apr 15 '18 at 03:56
  • From the idea-perspective, this is kind of exactly what I want. Just that I only want the teamspeak service not to be killed on exit. All other processes that have been started (perhaps even accidently) shall still be killed, when they are not needed anymore. Also I see what @FilipeBrandenburger is trying to say and he probably is right. Though I can't say, since I'm an systemd service amateur ^^ – Apahdos Apr 15 '18 at 15:19
  • @filbranden it work's. See also [this archwiki article](https://wiki.archlinux.org/index.php/Systemd/User). For my user scope systemd service, this stopped the service from terminating when closing the last session. – Xaser Feb 23 '19 at 00:30
  • 1
    thank you, that helped me! – ata Apr 26 '20 at 08:27
0

I think this service should not run for the specific user/group (teamspeak).

To start, backup all your files (service file, etc.) and then disable it:

# systemctl disable teamspeak.service

also, remove/delete the physical teamspeak.service file you copied on the system.

ssh into your server as root user, and try this:

Edit your teamspeak.service file:

[Unit]
Description=TeamSpeak 3 Server
After=network.target

[Service]
ExecStart=/teamspeak/ts3server_startscript.sh start
ExecStop=/teamspeak/ts3server_startscript.sh stop
RestartSec=1s


[Install]
WantedBy=default.target

Next, install systemd service unit and enable it so it will be executed at the boot time:

# cp  teamspeak.service /etc/systemd/system/
# chmod 0664 /etc/systemd/system/teamspeak.service
# systemctl daemon-reload
# systemctl enable teamspeak.service

start the service:

# systemctl start teamspeak.service

look for the OK response (green dot).

to find if service exists, or is configured:

# systemctl list-unit-files --type=service | grep teamspeak

enquire about service:

# service teamspeak status

Note: systemd reference URL - https://wiki.debian.org/systemd

scrat.squirrel
  • 303
  • 2
  • 7
  • I kinda do not quite like this answer. I defenitly do not want my teamspeak server to run as root. For security reasons obviously. Also afaik, the teamspeak startscript forbids root user to start the server. There is a part in the script that sais something like "if userid=0 echo "you should not run the server as root" and then exits without starting the server. And if I read and understand your answer correctly, This is exactly what the service would do, If I do it this way – Apahdos Apr 15 '18 at 15:16