[LINUX] Start script (for binaries)

The simple start script included with the TeamSpeak 3 server, modified to work with the TeamSpeak 6 server (beta).

Usage: ./tsserver_startscript.sh {start|stop|restart|status}

#!/bin/sh
# Copyright (c) 2019 TeamSpeak Systems GmbH
# All rights reserved

BINARYNAME=tsserver
COMMANDLINE_PARAMETERS="--accept-license" #add any command line parameters you want to pass here
PID_FILE=tsserver.pid

do_start() {
	if [ -e $PID_FILE ]; then
		PID=$(cat "$PID_FILE")
		if (ps -p "$PID" >/dev/null 2>&1); then
			echo "The server is already running, try restart or stop"
			return 1
		else
			echo "$PID_FILE found, but no server running. Possibly your previously started server crashed"
			echo "Please view the logfile for details."
			rm $PID_FILE
		fi
	fi
	if [ $(id -u) -eq 0 ]; then
		echo "WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT"
		c=1
		while [ "$c" -le 10 ]; do
			echo -n "!"
			sleep 1
			c=$(($c + 1))
		done
		echo "!"
	fi
	echo "Starting the TeamSpeak 6 server"
	if [ ! -e "$BINARYNAME" ]; then
		echo "Could not find binary, aborting"
		return 5
	fi
	if [ ! -x "$BINARYNAME" ]; then
		echo "${BINARYNAME} is not executable, trying to set it"
		chmod u+x "${BINARYNAME}"
	fi
	if [ ! -x "$BINARYNAME" ]; then
		echo "${BINARYNAME} is not exectuable, cannot start TeamSpeak 6 server"
		return 3
	fi
	"./${BINARYNAME}" "${@}" --daemon --pid-file "$PID_FILE"
	if [ $? -eq 0 ]; then
		echo "TeamSpeak 6 server started, for details please view the log file"
	else
		echo "TeamSpeak 6 server could not start"
		return 4
	fi
}

do_stop() {
	if [ ! -e $PID_FILE ]; then
		echo "No server running ($PID_FILE is missing)"
		return 0
	fi
	PID=$(cat "$PID_FILE")
	if (! ps -p "$PID" >/dev/null 2>&1); then
		echo "No server running"
		return 0
	fi

	echo -n "Stopping the TeamSpeak 6 server "
	kill -TERM "$PID" || exit $?
	rm -f $PID_FILE
	c=300
	while [ "$c" -gt 0 ]; do
		if (kill -0 "$PID" 2>/dev/null); then
			echo -n "."
			sleep 1
		else
			break
		fi
		c=$(($c - 1))
	done
	echo
	if [ $c -eq 0 ]; then
		echo "Server is not shutting down cleanly - killing"
		kill -KILL "$PID"
		return $?
	else
		echo "done"
	fi
	return 0
}

do_status() {
	if [ ! -e $PID_FILE ]; then
		echo "No server running ($PID_FILE is missing)"
		return 1
	fi
	PID=$(cat "$PID_FILE")
	if (! ps -p "$PID" >/dev/null 2>&1); then
		echo "Server seems to have died"
		return 1
	fi
	echo "Server is running"
	return 0
}

# change directory to the scripts location
cd $(dirname $([ -x "$(command -v realpath)" ] && realpath "$0" || readlink -f "$0"))

case "$1" in
start)
	shift
	do_start "$@" $COMMANDLINE_PARAMETERS
	exit $?
	;;
stop)
	do_stop
	exit $?
	;;
restart)
	shift
	do_stop && (do_start "$@" $COMMANDLINE_PARAMETERS)
	exit $?
	;;
status)
	do_status
	exit $?
	;;
*)
	echo "Usage: ${0} {start|stop|restart|status}"
	exit 2
	;;
esac

4 Likes

Does it not have by default?

No, but you can use screen to keep the TS server running after you leave the TeamSpeak terminal screen.

Not on VPSs tho… :slight_smile:

hm?

just install screen on your VPS

WTF? Is it even possible? I don’t think so, I was thinking it only had the terminal and couldn’t be possible to install an actual interface…

screen is not a actual interface

But you can also install a GUI on a headless Linux VPS. :smiley:

GOOGEL:

In Linux, screen is a full-screen window manager that allows you to manage multiple terminal sessions within a single physical terminal. It’s like having multiple tabs in your terminal, allowing you to switch between different processes (like running shells or other commands) without losing your current session. This is particularly useful for running long-running tasks, working remotely, or managing multiple processes simultaneously.

1 Like

Maybe you are thinking of screen as a graphical interface, but it’s just a program that allows you to run multiple sessions in the same terminal window.

In this case you would do:

screen ./tsserver

You’ll see the TeamSpeak server process running in the terminal and it’s log.

Then you can press Ctrl+A then D to deattach from that session, and it will keep running in the background.

You can then do:

screen -ls

And you’ll see that the server is running in the background.

If you want to bring back the session you can do:

screen -r

That is if it’s the only session running, if you have more than one you’ll have to pass the pid in the previous command.

That’s just out the top of my head, I haven’t used screen in a long time so I probably made a mistake.

The benefit of using a start script (like the one included with TS3) is that it will run the server with a deamon, allowing you to better control the status of it like restarting it on crash.

Maybe you could pass the same arguments the script does to screen.

screen ./tsserver --daemon --pid-file=tsserver.pid

But I’m not sure this would work since when you run the server in daemon mode it doesn’t display in the terminal, it just runs the server in the background and closes, there’s no output to be shown.

Either way, what works for you it’s best, but in production I wouldn’t rely on screen. Probably go for Docker or a combination of scripts.

1 Like

That’s very interesting I didn’t know you could run multiple sessions in one terminal. That’s going to be pretty useful since some of the servers only rely if it’s part of a session, and I can’t actually use daemon for that. I’ll give it a try soon.