Skip to content

Instantly share code, notes, and snippets.

@JuanPotato
Forked from jdavis/terrariad
Last active January 31, 2019 02:42
Show Gist options
  • Save JuanPotato/bba9d25e9ba7b8b58a2692d541af89cc to your computer and use it in GitHub Desktop.
Save JuanPotato/bba9d25e9ba7b8b58a2692d541af89cc to your computer and use it in GitHub Desktop.
Simple init.d Service script to start a Terraria Server
#!/bin/sh
#
# Terraria Config
#
#maxplayers=69
#world=/home/terrariaserver/.local/share/Terraria/Worlds/LetsNotFail.wld
#port=7777
#password=potato
#motd=Potato was here!
#worldpath=/home/terrariaserver/.local/share/Terraria/Worlds/
#secure=0
# Tmux session ID
SESSION=terrariaserver
# Username to run as
USERNAME=terrariaserver
# Location of Terraria files
TERDIR=/home/terrariaserver/Dedicated\ Server/Linux
# Server executable
SERVER=${TERDIR}/TerrariaServer.bin.x86_64
# Server Config
CONFIG=${TERDIR}/server.conf
# World to load
WORLD=/home/terrariaserver/.local/share/Terraria/Worlds/LetsNotFail.wld
# Source function library.
. /etc/rc.d/init.d/functions
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
start() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
ARGS="-config ${CONFIG}"
CMD="${SERVER} ${ARGS}"
echo "Starting Terraria Server... "
as_user "tmux new-session -d -s $SESSION"
as_user "tmux send-keys -t ${SESSION} \"$CMD\" C-m"
}
stop() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
as_user "tmux send-keys -t ${SESSION} \"exit\" C-m"
echo "Stopped Terraria Server"
}
restart() {
stop
start
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment