Automatic restarts for a Team Fortress 2 server
A Team Fortress 2 server keeps nothing persistent, so a restart only clears state and loads new config — there is nothing to protect. For most community servers, once a week is ample.
When should it restart?
Put {{minutes}} wherever the number of minutes left should appear.
What happens, in order
- Job startsWarn
say Server restarting in 15 min - 10 min beforeWarn
say Server restarting in 10 min - 5 min beforeWarn
say Server restarting in 5 min - 1 min beforeWarn
say Server restarting in 1 min - Restart timeStop
quit
Nothing to save. Time the restart for a map change if you can.
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-tf2.sh
The script it runs
Save it as /opt/restart-tf2.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 Team Fortress 2.
# 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:-27015}"
: "${RCON_PASSWORD:?set RCON_PASSWORD before running}"
send() {
rcon-cli --host "$RCON_HOST" --port "$RCON_PORT" --password "$RCON_PASSWORD" "$1"
}
# 15 minute warning
send 'say Server restarting in 15 min'
sleep 300
# 10 minute warning
send 'say Server restarting in 10 min'
sleep 300
# 5 minute warning
send 'say Server restarting in 5 min'
sleep 240
# 1 minute warning
send 'say Server restarting in 1 min'
sleep 70
# Shut down cleanly
send 'quit'
# Give the process time to finish writing before anything restarts it.
sleep 30
systemctl start tf2 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 Team Fortress 2
Ideally the restart lands between maps, not in the middle of a round. Cron cannot see what the game is doing, so in practice you choose an hour when the server is normally empty and accept that now and then it will not be. With custom maps, players returning after a restart have nothing to download again, but they do all have to reconnect — and on a busy server that shows up as a dip.
Advertisement