Advertisement

Automatic restarts for a Factorio server

Of every game on this list, Factorio needs a scheduled restart the least. It is designed to run for weeks at a time, its memory tracks the size of the factory rather than the uptime, and a headless server left to its own devices will just carry on.

When should it restart?

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

What happens, in order

  1. Job startsWarnServer restarting in 15 min
  2. 10 min beforeWarnServer restarting in 10 min
  3. 5 min beforeWarnServer restarting in 5 min
  4. 1 min beforeWarnServer restarting in 1 min
  5. Restart timeSave/server-save
  6. Restart timeStop/quit

Plain text sent over Factorio RCON is printed straight into chat, which is why the broadcast line has no command in front of it. Anything starting with a slash is treated as a command instead.

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

The script it runs

Save it as /opt/restart-factorio.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 Factorio.
# 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 'Server restarting in 15 min'

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

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

sleep 240
# 1 minute warning
send '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 factorio 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 Factorio

So schedule it for the save, not the restart. Putting /server-save on a timer leaves you a rolling series of recovery points, which autosave by itself does not. One quirk of the warning step: Factorio's RCON drops any plain text straight into chat, and treats anything starting with a slash as a command — so the warning line deliberately has no command in front of it.

Advertisement

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