Advertisement

Automatic restarts for a Rust server

Over a wipe, a Rust server's entity count only goes up — each foundation, each sleeping bag and each item on the ground is something it has to simulate. Late in a wipe the server measurably runs worse than it did on day one, and a restart each day wins back a worthwhile chunk of that.

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 timeSaveserver.save
  6. Restart timeStopquit

quit saves before exiting. Rust writes large save files, so allow the process a minute to finish rather than following up with a kill.

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

The script it runs

Save it as /opt/restart-rust.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 Rust.
# 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:-28016}"
: "${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 'server.save'

sleep 10
# Shut down cleanly
send 'quit'

# Give the process time to finish writing before anything restarts it.
sleep 30
systemctl start rust 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 Rust

Rust's save file is big and writing it takes a while. Do not chase the quit with a kill or a container restart on a tight timer — allow the process a minute at the very least. Also think about when: restarting at peak time empties the server, and players dropped halfway through a raid tend not to come back, so pick the quietest hour your community has.

Advertisement

Setting up RCON

The script talks to the server over RCON, configured on the launch line with +rcon.port (28016 by default), +rcon.password and +rcon.web 1. That last flag selects WebRCON, the websocket variant almost every Rust admin tool expects. Use a long random password: Rust RCON is among the most scanned services in gaming, and even with the port closed, a weak password is a risk to anyone on the same network.

A restart is not a wipe

A restart keeps the map and every base on it. A wipe means deleting the map save in server/<identity>/, and a blueprint wipe also deletes the player.blueprints database files there. Keep the two jobs apart: a nightly restart script should never delete anything, and a wipe script should run on its own schedule, by hand or on wipe day only.

Update before you start

Put the SteamCMD update for app 258550 between the stop and the start. Most nights it finds nothing to do, but on a patch day it is what keeps a restarted server from coming back on an old version that no client can join.

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