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.

12
  • You also need to tell answerers where this service unit file is, and exactly how you enabled it.
    – JdeBP
    Apr 14, 2018 at 9: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, 2018 at 4:02
  • @JdeBP added now, thanks for reminding. Sorry that I haven't thought of that right away.
    – Apahdos
    Apr 15, 2018 at 15:13
  • @FilipeBrandenburger added now, I hope this is helpful. Thanks for reminding though.
    – Apahdos
    Apr 15, 2018 at 15:14
  • 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, 2018 at 9:42

2 Answers 2

6

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

sudo loginctl enable-linger teamspeak
4
  • 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, 2018 at 3: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, 2018 at 15:19
  • @filbranden it work's. See also this archwiki article. For my user scope systemd service, this stopped the service from terminating when closing the last session.
    – Xaser
    Feb 23, 2019 at 0:30
  • 1
    thank you, that helped me!
    – ata
    Apr 26, 2020 at 8: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

1
  • 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, 2018 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .