Why Auto-Restart Matters
Game servers can crash due to memory leaks, mod conflicts, world corruption, or unexpected errors. Automated crash detection and restart ensures your players experience minimal downtime without requiring manual intervention at 3 AM.
Systemd Restart Policies
# /etc/systemd/system/gameserver.service
[Unit]
Description=Game Server
After=network.target
[Service]
User=gameserver
WorkingDirectory=/opt/gameserver
ExecStart=/opt/gameserver/start.sh
Restart=always
RestartSec=15
StartLimitIntervalSec=300
StartLimitBurst=5
# Notify on restart
ExecStartPost=/usr/local/bin/notify-restart.sh
[Install]
WantedBy=multi-user.target
Custom Crash Detection Script
#!/bin/bash
# /usr/local/bin/watchdog.sh
SERVER_NAME="Minecraft"
PROCESS_NAME="java"
CHECK_PORT=25565
LOG="/var/log/gameserver-watchdog.log"
WEBHOOK="https://discord.com/api/webhooks/YOUR/WEBHOOK"
log() { echo "[$(date)] $1" >> "$LOG"; }
while true; do
# Check if process is running
if ! pgrep -f "$PROCESS_NAME.*server.jar" > /dev/null; then
log "CRASH: $SERVER_NAME process not found"
systemctl restart gameserver
curl -H "Content-Type: application/json" -d \
"{\"content\":\"$SERVER_NAME crashed and was restarted\"}" \
"$WEBHOOK"
sleep 60 # Wait for server to start
continue
fi
# Check if port is responding
if ! timeout 10 bash -c "echo > /dev/tcp/127.0.0.1/$CHECK_PORT" 2>/dev/null; then
log "HANG: $SERVER_NAME not responding on port $CHECK_PORT"
systemctl restart gameserver
curl -H "Content-Type: application/json" -d \
"{\"content\":\"$SERVER_NAME was unresponsive and restarted\"}" \
"$WEBHOOK"
sleep 60
continue
fi
sleep 30
done
Memory-Based Restart
#!/bin/bash
# Restart if memory usage exceeds threshold
MAX_MEM_MB=7500
PID=$(pgrep -f "server.jar")
if [ -n "$PID" ]; then
MEM_MB=$(ps -o rss= -p $PID | awk "{print \$1/1024}")
if (( $(echo "$MEM_MB > $MAX_MEM_MB" | bc -l) )); then
echo "Memory ${MEM_MB}MB exceeds ${MAX_MEM_MB}MB, restarting..."
systemctl restart gameserver
fi
fi
Scheduled Restarts
# Prevent memory leaks with daily restarts
# /usr/local/bin/scheduled-restart.sh
#!/bin/bash
SCREEN_NAME="minecraft"
# Warn players
screen -S $SCREEN_NAME -X stuff "say Server restarting in 5 minutes!\n"
sleep 240
screen -S $SCREEN_NAME -X stuff "say Server restarting in 1 minute!\n"
sleep 50
screen -S $SCREEN_NAME -X stuff "say Server restarting in 10 seconds!\n"
sleep 10
screen -S $SCREEN_NAME -X stuff "stop\n"
# Systemd will auto-restart the server
# Cron: 0 4 * * * /usr/local/bin/scheduled-restart.sh
Discord Notifications
#!/bin/bash
# /usr/local/bin/notify-restart.sh
WEBHOOK="https://discord.com/api/webhooks/YOUR/WEBHOOK"
curl -s -H "Content-Type: application/json" \
-d "{\"embeds\":[{\"title\":\"Server Restarted\",\"description\":\"The game server has been restarted.\",\"color\":65280,\"timestamp\":\"$(date -Iseconds)\"}]}" \
"$WEBHOOK"
Best Practices
- Use systemd Restart=always with reasonable RestartSec (15-30s)
- Set StartLimitBurst to prevent infinite restart loops
- Monitor memory and CPU usage to predict crashes
- Schedule daily restarts during low-traffic hours
- Send notifications on crash/restart for awareness
- Log crash events for pattern analysis