I have set up my raspberry pi running ubuntu 20.04 server so it can run teamspeak with box86.
To do that I set up a chroot in ls /srv/chroot/focal-armhf/
with debootstrap. In that directory I compiled box86. I then downloaded and installed ts3server for x86 on linux. I have set up a custom config for schroot.
/etc/schroot/chroot.d/focal-armhf.conf
:
[focal-armhf]
description=Ubuntu focal armhf chroot
type=directory
directory=/srv/chroot/focal-armhf
profile=ts3server
personality=linux
# preserve-environment=true
root-users=ubuntu
users=ubuntu
When I run the following commands in bash on the host, everything works fine.
# beginn session, name it ts3server
sudo /usr/bin/schroot -n ts3server -b -c chroot:focal-armhf
# run start script. it's the same as the default start script, however I added /usr/local/bin/box86 in front of the starting of the actual server binary.
sudo /usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' start
# at this point the server is running and i can connect to it.
# ...
# when I want to shut down:
# stopping server
/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' stop
# ending schroot session
/usr/bin/schroot -e --chroot ts3server
I wanted to do this automatically with systemd now. So I created this unit configuration
[Unit]
Description=Teamspeak3 Server in chroot
After=network.target
After=syslog.target
[Service]
ExecStartPre=/usr/bin/schroot -n ts3server -b -c chroot:focal-armhf
ExecStart=/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' start
ExecStop=/usr/bin/schroot --run-session --chroot ts3server -d /opt/ts3server/teamspeak3-server_linux_x86/ './ts3custom.sh' stop
ExecStopPost=/usr/bin/schroot -e --chroot ts3server
Restart=no
Type=simple
[Install]
WantedBy=multi-user.target
When I run it however it shuts down immediately after. The teamspeak server log runs is
2021-05-06 17:58:49.429511|INFO |ServerLibPriv | |TeamSpeak 3 Server 3.13.3 (2020-12-16 14:17:05)
2021-05-06 17:58:49.456286|INFO |ServerLibPriv | |SystemInformation: Linux 5.4.0-1034-raspi #37-Ubuntu SMP PREEMPT Mon Apr 12 23:14:49 UTC 2021 i686 Binary: 32bit
2021-05-06 17:58:49.456975|WARNING |ServerLibPriv | |The system locale is set to "C" this can cause unexpected behavior. We advice you to repair your locale!
2021-05-06 17:58:49.508403|INFO |DatabaseQuery | |dbPlugin name: SQLite3 plugin, Version 3, (c)TeamSpeak Systems GmbH
2021-05-06 17:58:49.509804|INFO |DatabaseQuery | |dbPlugin version: 3.11.1
2021-05-06 17:58:49.553828|INFO |DatabaseQuery | |checking database integrity (may take a while)
If I set restart always, it will keep generating that same log over and over again.
It seems like the server is started, then forks to the background, then ExecStop is called by systemd.
I have tried setting Type=forking
and Type=exec
and Type=simple
, all with the same results.
Basically everything works fine, until I try to use systemd to manage it all.
How can I make it work?
In particular I want:
- Automatically start it with the system
- Manage it with
service ts3server start|stop|restart
For completeness sake my custom script to manage teamspeak server:
#!/bin/sh
# Copyright (c) 2019 TeamSpeak Systems GmbH
# All rights reserved
BINARYNAME="./ts3server"
COMMANDLINE_PARAMETERS="" #add any command line parameters you want to pass here
PID_FILE=ts3server.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 3 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 "${BINARNAME} is not exectuable, cannot start TeamSpeak 3 server"
return 3
fi
/usr/local/bin/box86 "./${BINARYNAME}" "${@}" "daemon=1" "pid_file=$PID_FILE"
if [ $? -eq 0 ]; then
echo "TeamSpeak 3 server started, for details please view the log file"
else
echo "TeamSpeak 3 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 3 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
I've had a similar problem with tmux and solved it like this:
It looked like the service was ended since its parent (the service) stopped. RemainAfterExit and KillMode helped...