Automatic restarts for a Terraria server
Terraria servers are lightweight, so a scheduled restart is mainly about rotating saves and clearing out whatever a long-running process has collected. It is a cheap habit, not a requirement.
When should it restart?
Put {{minutes}} wherever the number of minutes left should appear.
What happens, in order
- Job startsWarn
/broadcast Server restarting in 15 min - 10 min beforeWarn
/broadcast Server restarting in 10 min - 5 min beforeWarn
/broadcast Server restarting in 5 min - 1 min beforeWarn
/broadcast Server restarting in 1 min - Restart timeSave
/save - 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
Moved 15 minutes ahead of the time you picked, so the restart happens at that time instead of 15 minutes later.
45 3 * * * /opt/restart-terraria.sh
The script it runs
Save it as /opt/restart-terraria.sh and mark it executable. It expects rcon-cli on the path and takes the password from an environment variable instead of storing it in the file.
#!/usr/bin/env bash
set -euo pipefail
# Scheduled restart for Terraria.
# Generated by spawnbench.com/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 || trueHeads up: cron follows the server clock, not your local time. Check with timedatectl first. A container set to UTC and an admin living in Europe differ by two hours in summer and by one in winter, so 04:00 may not be the 04:00 you had in mind.
Advertisement
What is different about Terraria
The commands here are TShock commands. Vanilla Terraria has a local console and no remote access whatsoever, so on vanilla none of this applies and you are left with a screen or tmux session and a keyboard. On TShock, make sure it is /off and not /off-nosave — they differ by a few characters, and one of them throws away everything since the last save.
Advertisement
Restarting a vanilla server
Without TShock there is no remote console, but there is a local one, and a script can type into it if the server runs inside tmux. The vanilla commands are say for a message, save to write the world, and exit, which saves and quits. A warning looks like tmux send-keys -t terraria "say Restarting in 5 minutes" Enter, followed later by the same for save and exit. Never send exit-nosave from a script; it quits without writing anything.
Backups between restarts
Each save keeps the previous version of the world next to it as a .wld.bak file, which is one step of undo and no more. TShock can do better: BackupInterval and BackupKeepFor in its config.json, both in minutes, take timed backups and decide how long to keep them. If you run vanilla, copy the .wld file somewhere else after each scheduled save instead.
Check that the save happened
After a scheduled restart, look at the modified time of the .wld file. It should match the moment the server stopped. If it is older, the stop is cutting the save short, or the script is sending a command the server does not know, which on vanilla means a TShock command such as /off that the plain console never understood.