Schedule a Terraria server restart

A Terraria server is light, and a scheduled restart is mostly about the save rotation and about clearing whatever a long-lived process has accumulated. It is a low-cost habit rather than a necessity.

When should it restart?

Use {{minutes}} where the countdown number should go.

What happens, in order

  1. Job startsWarn/broadcast Server restarting in 15 min
  2. 10 min beforeWarn/broadcast Server restarting in 10 min
  3. 5 min beforeWarn/broadcast Server restarting in 5 min
  4. 1 min beforeWarn/broadcast Server restarting in 1 min
  5. Restart timeSave/save
  6. Restart timeStop/off

These are TShock commands — a vanilla Terraria server has only its local console and no remote access at all. /off saves on the way out; /off-nosave is the one to avoid.

The crontab line

Shifted 15 minutes earlier than your chosen time, so the restart itself lands on it rather than 15 minutes after.

45 3 * * *  /opt/restart-terraria.sh

The script it runs

Save as /opt/restart-terraria.sh and make it executable. It assumes rcon-cli is on the path and reads the password from the environment rather than embedding it.

#!/usr/bin/env bash
set -euo pipefail

# Scheduled restart for Terraria.
# Generated by spawnly.net/tools/restart-scheduler

# The password stays out of this file. Put it in the environment, or in a
# file only root can read, and export it before this script runs.
RCON_HOST="${RCON_HOST:-127.0.0.1}"
RCON_PORT="${RCON_PORT:-7777}"
: "${RCON_PASSWORD:?set RCON_PASSWORD before running}"

send() {
  rcon-cli --host "$RCON_HOST" --port "$RCON_PORT" --password "$RCON_PASSWORD" "$1"
}

# 15 minute warning
send '/broadcast Server restarting in 15 min'

sleep 300
# 10 minute warning
send '/broadcast Server restarting in 10 min'

sleep 300
# 5 minute warning
send '/broadcast Server restarting in 5 min'

sleep 240
# 1 minute warning
send '/broadcast Server restarting in 1 min'

sleep 60
# Flush the world to disk
send '/save'

sleep 10
# Shut down cleanly
send '/off'

# Give the process time to finish writing before anything restarts it.
sleep 30
systemctl start terraria 2>/dev/null || true

Cron uses the server’s timezone, not yours. Check it with timedatectl before assuming 04:00 means 04:00 where you are — a container in UTC and an admin in Europe are two hours apart in summer and one in winter.

The part specific to Terraria

These commands are TShock's — a vanilla Terraria server has only its local console and no remote access at all, so if you are running vanilla, none of this applies and your options are a screen or tmux session and a keyboard. Within TShock, be careful to use /off rather than /off-nosave; the two are one keystroke apart and one of them discards everything since the last save.

Support & Help