Schedule a Minecraft server restart

A Minecraft server does not need restarting the way people assume, but it benefits from it anyway. Memory fragments over long uptimes, chunk data accumulates in ways the garbage collector handles less and less gracefully, and plugins with leaks are common enough that a nightly restart is cheaper than finding which one is at fault.

When should it restart?

Use {{minutes}} where the countdown number should go.

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 timeSavesave-all
  6. Restart timeStopstop

stop flushes the world to disk on its way out, so the explicit save-all is belt and braces rather than strictly required. Keep it anyway — it costs nothing and it means the save has already happened if the shutdown hangs.

The crontab line

Shifted 15 minutes earlier than your chosen time, so the restart itself lands on it rather than 15 minutes after.

45 3 * * *  /opt/restart-minecraft.sh

The script it runs

Save as /opt/restart-minecraft.sh and make it executable. It assumes rcon-cli is on the path and reads the password from the environment rather than embedding it.

#!/usr/bin/env bash
set -euo pipefail

# Scheduled restart for Minecraft.
# Generated by spawnly.net/tools/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:-25575}"
: "${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 'save-all'

sleep 10
# Shut down cleanly
send 'stop'

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

Cron uses the server’s timezone, not yours. Check it with timedatectl before assuming 04:00 means 04:00 where you are — a container in UTC and an admin in Europe are two hours apart in summer and one in winter.

The part specific to Minecraft

Send save-all and let it finish before stop. The shutdown does save on its own, so this is redundant in the normal case — but if the process hangs on the way out, the difference between a redundant save and no save is the difference between losing nothing and losing everything since the last autosave. If you run a proxy such as Velocity or BungeeCord, restart the backend servers on a stagger rather than together, so players are moved rather than disconnected.

Support & Help