Skip to content

Instantly share code, notes, and snippets.

@dekarrin
Last active May 29, 2021 01:20
Show Gist options
  • Save dekarrin/94da66b9679e7e209eec151b4a093615 to your computer and use it in GitHub Desktop.
Save dekarrin/94da66b9679e7e209eec151b4a093615 to your computer and use it in GitHub Desktop.
garbage script cobbled together to setup a terraria server and provide systemd unit file
#!/bin/bash
# note: RUNNING THIS SCRIPT COULD DESTROY EXTANT SERVERS THAT ARE SAME VERSION
# stop script on first error
set -e
# sudo check
if [ "$(id -u)" -ne 0 ]
then
echo "You must be root to run this script; try again with sudo" >&2
exit 1
fi
# if they didnt pass the version in as first argument, prompt for it
if [ -n "$1" ]
then
terraria_version="$1"
else
read -rp "What version of Terraria to install? " terraria_version
fi
# create user to own terraria server process(s)
echo "Creating system account to run terraria server..."
useradd -r terraria 2>/dev/null || echo "User 'terraria' already exists, not creating again"
usermod -d /etc/terraria terraria 2>/dev/null
addgroup terraria-ops 2>/dev/null || echo "Group 'terraria-ops' already exists, not creating again"
usermod -a -G terraria-ops terraria 2>/dev/null
# create generic base directory for all server versions
if [ ! -e /etc/terraria ]
then
echo "Creating server directory in /etc/terraria..."
mkdir /etc/terraria
else
echo "Server directory in /etc/terraria already exists, skipping creation..."
fi
# move into terraria dir and do all further work there
cd /etc/terraria
# make worlds dir if not exists
if [ ! -d worlds ]
then
echo "Creating worlds directory in /etc/terraria/worlds..."
mkdir worlds
chmod 775 worlds
else
echo "Worlds directory already exists in /etc/terraria/worlds; skipping creation"
fi
# find the server archive
echo "Locating server download URL..."
collapsed_ver="$(echo "$terraria_version" | sed -e 's/\.//g')"
server_archive_name="terraria-server-${collapsed_ver}.zip"
# this horrible kludge is because terraria doesn't really have a cdn with
# well-formed and consistent URLs; that's fine, we'll just download the page of the
# wiki which has them all and crawl it to find the right URL, since the archive name
# IS well-formed we can look for that.
curl -L 'https://terraria.fandom.com/wiki/Server#Downloads' > wiki.html
server_url="$(grep "$server_archive_name" wiki.html | sed -e 's/^.*href="\(.*$\)/\1/' | sed -e 's/\(^.*\)">.*$/\1/')"
rm wiki.html
echo "Server archive is located at $server_url"
# download and extract server archive
echo "Downloading server..."
curl -LO "$server_url"
echo "Extracting server..."
unzip "$server_archive_name"
rm "$server_archive_name"
echo "Setting up server..."
cd "${collapsed_ver}"
# erase non-linux versions
rm -r Mac Windows
cd Linux
# set permissions
chmod 760 "TerrariaServer"*
read -srp "What should the server password be? " server_pass
# create config, note that the use of relative paths here
# makes it so server needs to run from the particular working dir
mkdir conf
cat <<- EOF > "conf/terraria.cfg"
################################
# Game settings. Set as needed #
################################
# message of the day
motd=We own fucking *helicopters* bitch!
# maximum number of players allowed in server
maxplayers=8
# anti-cheat protection
secure=1
# server password
password=$server_pass
# server language
language=en/US
# set this if you have a world you want to autostart with.
# you MUST connect to the console to setup your world
# first or import an existing one
# world=/etc/terraria/worlds/world-file.wld
################################################
# Path settings. Set to where these files are. #
################################################
# where the worlds are saved to and loaded from
worldpath=/etc/terraria/worlds
# location of banlist
banlist=conf/banlist.txt
#########################################
# network settings, not usually changed #
#########################################
# listening port
port=7777
# disable automatic port-forwarding
upnp=0
# set how often npc updates are sent, presumably per second.
# less is less 'skipping around' but more bandwidth. Lower if
# enemies are skipping around, raise if you are using too much
# bandwidth
npcstream=60
EOF
chown terraria:terraria /etc/terraria
chown -R terraria:terraria-ops /etc/terraria/*
chmod -R 770 /etc/terraria/$collapsed_ver/Linux/conf
# setup the socket
mkdir /var/terraria
chgrp terraria-ops /var/terraria
chmod g+ws /var/terraria
sudo -u terraria HOME=/etc/terraria -s tmux -S /var/terraria/sock
terraria_version=1.4.2.3
collapsed_ver=1423
# oh my god this is a mess this should NOT be in a bash script jfc deka
# and yet here it is. honk.
echo "[Unit]" > ~/terraria.service
echo "Description=Terraria v$terraria_version" >> ~/terraria.service
cat <<- 'EOF' >> ~/terraria.service
[Service]
User=terraria
Type=forking
EOF
echo WorkingDirectory=/etc/terraria/$collapsed_ver/Linux >> ~/terraria.service
cat <<- 'EOF' >> ~/terraria.service
ExecStartPre=/bin/bash -c "tmux -S /var/terraria/sock send-keys -t terraria 'exit' enter >/dev/null 2>&1 || echo 'No console needs closing'"
ExecStart=/bin/bash -c "sleep 1 && tmux -S /var/terraria/sock new-session -s terraria -d"
ExecStartPost=/bin/bash -c "sleep 3"
ExecStartPost=/bin/bash -c "tmux -S /var/terraria/sock send-keys -t terraria './TerrariaServer -config conf/terraria.cfg ; exit $?' enter"
ExecStop=/bin/bash -c "tmux -S /var/terraria/sock send-keys -t terraria 'exit' enter"
ExecStop=/bin/bash -c "sleep 10"
[Install]
WantedBy=multi-user.target
EOF
mv ~/terraria.service /etc/systemd/system
systemctl daemon-reload
systemctl start terraria || systemctl status terraria
@dekarrin
Copy link
Author

oh my god do not run this as a script you will regret it

this is so so broken atm. each step can be looked at tho ig. glub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment