Advertisement

Automatic restarts for a 7 Days to Die server

Memory on a 7 Days to Die server follows how much of the map players have explored and built on, and neither number ever falls. A daily restart keeps things snappy across a long playthrough — above all on horde nights, which is when everybody turns up.

When should it restart?

Put {{minutes}} wherever the number of minutes left should appear.

What happens, in order

  1. Job startsWarnsay "Server restarting in 15 min"
  2. 10 min beforeWarnsay "Server restarting in 10 min"
  3. 5 min beforeWarnsay "Server restarting in 5 min"
  4. 1 min beforeWarnsay "Server restarting in 1 min"
  5. Restart timeSavesaveworld
  6. Restart timeStopshutdown

The console here is telnet rather than RCON. Reach it over an SSH tunnel or from the machine itself — the telnet port must not be forwarded, because it authenticates with a password in clear text.

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-seven_days_to_die.sh

The script it runs

Save it as /opt/restart-seven_days_to_die.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 7 Days to Die.
# 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:-8081}"
: "${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 60
# Flush the world to disk
send 'saveworld'

sleep 10
# Shut down cleanly
send 'shutdown'

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

Heads 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 7 Days to Die

This game's console is telnet rather than RCON, and telnet transmits its password unencrypted. Run the script on the server machine, or reach the port through an SSH tunnel; never forward it. And keep restarts clear of day seven and every multiple of it: a restart in the middle of a blood moon is the worst timing there is, and the schedule has no way of knowing unless you plan around it.

Advertisement

Setting up telnet for the script

The script needs the telnet console, enabled with TelnetEnabled in serverconfig.xml on port 8081. With TelnetPassword left empty, the server only accepts telnet connections from the machine itself, which is exactly what a local restart script needs and is safer than setting a password and exposing the port. The sequence is say for each warning, saveworld, then shutdown.

Planning around the Blood Moon

Horde night comes every seven days unless the sandbox options say otherwise, and it is the worst possible moment for a restart. Since V3.0 that frequency is part of the SandboxCode, not a BloodMoonFrequency line, so check the in-game sandbox options if you are not sure what your server uses. A restart at a fixed real-world hour does not line up with the in-game calendar: at the default hour-long day, every real hour moves the world forward a whole day.

Updates inside a restart script

Adding a SteamCMD update for app 294420 to the restart keeps the server current, but on 7 Days to Die that can pull in a major version your save is not ready for. Pin the server to a branch with +app_update 294420 -beta followed by the branch name, and move to a new major version by hand, after a backup.

Rather not run the server yourself?

These tools are free and funded by the two hosting services we run. If you want a server that is already set up, here they are.

Advertisement