Skip to content

Instantly share code, notes, and snippets.

@rtizzy
Forked from jdavis/terrariad
Created July 7, 2016 06:09
Show Gist options
  • Save rtizzy/7b5ee41c8f578426b36f11bfa2a97b3f to your computer and use it in GitHub Desktop.
Save rtizzy/7b5ee41c8f578426b36f11bfa2a97b3f to your computer and use it in GitHub Desktop.

Revisions

  1. @jdavis jdavis created this gist Dec 28, 2013.
    80 changes: 80 additions & 0 deletions terrariad
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #!/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 $?