Setting Up Automatic Restarts and Crash Recovery

ZCraft Studios

A server that crashes at 3 AM and stays down until someone notices costs more uptime than almost any other single failure.

Setting Up Automatic Restarts and Crash Recovery

The server crashing is inevitable eventually. Staying down for six hours because nobody was awake to restart it is the part that's actually preventable.

A simple restart loop around the server's start command means a crash results in a few seconds of downtime instead of however long until someone notices and manually restarts it.

A Basic Auto-Restart Loop

Instead of running the server's start command directly, wrap it in a loop that restarts it whenever the process exits:

#!/bin/bash
cd /home/mc/myserver

while true; do
    java -Xms4G -Xmx4G -jar paper.jar nogui
    echo "Server stopped — restarting in 5 seconds..."
    sleep 5
done

Save this as start-loop.sh, make it executable (chmod +x start-loop.sh), and run it inside a screen or tmux session so it keeps running after disconnecting.

Distinguishing a Crash From an Intentional Stop

The loop above restarts on any exit, including an intentional /stop from an admin — which usually isn't what's wanted. A common fix is checking for a "stop file" the restart script respects:

#!/bin/bash
cd /home/mc/myserver
rm -f ./.stop-requested

while true; do
    java -Xms4G -Xmx4G -jar paper.jar nogui

    if [ -f "./.stop-requested" ]; then
        echo "Intentional stop detected — not restarting."
        break
    fi

    echo "Unexpected exit — restarting in 5 seconds..."
    sleep 5
done

An admin creates .stop-requested (via a plugin, or manually) before running /stop, and the loop respects it instead of restarting.

Running It as a systemd Service (Linux)

For a more robust setup than a manual screen session, a systemd service handles both auto-restart and starting on boot:

[Unit]
Description=Minecraft Server
After=network.target

[Service]
WorkingDirectory=/home/mc/myserver
ExecStart=/usr/bin/java -Xms4G -Xmx4G -jar paper.jar nogui
Restart=on-failure
RestartSec=5
User=mc

[Install]
WantedBy=multi-user.target

Save as /etc/systemd/system/minecraft.service, then:

sudo systemctl enable minecraft
sudo systemctl start minecraft

Restart=on-failure handles the crash-vs-intentional-stop distinction automatically — a clean /stop exits with a success code and isn't restarted, while a crash exits with a failure code and is.

Scheduled Restarts (Not Just Crash Recovery)

Many servers also restart on a schedule (daily or every few hours) regardless of crashes, to clear accumulated memory fragmentation and apply any pending plugin updates. A cron job sending a graceful /stop (with a warning broadcast beforehand) at a fixed time, combined with either of the methods above, covers both scheduled and crash-driven restarts with one setup.

Monitoring, Not Just Recovering

An auto-restart loop solves downtime duration but not downtime visibility — a logging line, a webhook to Discord on each unexpected restart, or basic uptime monitoring (even a simple external ping check) means repeated crashes actually get noticed and investigated instead of silently restarting forever.

Share: