Skip to content

Instantly share code, notes, and snippets.

@jdavis
Created December 28, 2013 21:23
Show Gist options
  • Save jdavis/8164358 to your computer and use it in GitHub Desktop.
Save jdavis/8164358 to your computer and use it in GitHub Desktop.
Simple init.d Service script to start a tshock Terraria Server
#!/bin/sh
#
# Terraria Config
#
# Tmux session ID
SESSION=terraria
# Username to run as
USERNAME=terraria
# Location of Terraria files
TERDIR=/home/terraria/Terraria
# Server executable
SERVER=${TERDIR}/TerrariaServer.exe
# Server Config
CONFIG=${TERDIR}/server.conf
# World to load
WORLD=${TERDIR}/Worlds/${WORLD_NAME}.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="-ip 0.0.0.0 -config ${CONFIG}"
CMD="mono ${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