Setting up your Minecraft server to auto-start on boot and restart if it crashes is pretty straightforward with systemd.
- Auto-starts when your Pi boots up
- Automatically restarts if the server crashes
- Easy to manage with simple commands
- Proper logging through journalctl
It's safer to run the server as a dedicated user (not root):
sudo useradd -r -m -d /opt/minecraft minecraft
sudo chown -R minecraft:minecraft /opt/minecraft/serverCopy the service file from this repo to systemd:
sudo cp configs/minecraft.service /etc/systemd/system/Or create it manually at /etc/systemd/system/minecraft.service:
[Unit]
Description=Minecraft Server 1.21.10
After=network.target
[Service]
Type=simple
User=minecraft
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -Xms2G -Xmx5G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -jar fabric-server-1.21.10.jar nogui
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.targetNote: Adjust the -Xms2G -Xmx5G values based on your Pi's RAM:
- Pi with 4GB RAM:
-Xms1G -Xmx3G - Pi with 8GB RAM:
-Xms2G -Xmx5G
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable auto-start on boot
sudo systemctl enable minecraft
# Start the server now
sudo systemctl start minecraft# Start the server
sudo systemctl start minecraft
# Stop the server
sudo systemctl stop minecraft
# Restart the server
sudo systemctl restart minecraft
# Check if it's running
sudo systemctl status minecraft
# View live logs
sudo journalctl -u minecraft -f
# View recent logs
sudo journalctl -u minecraft --since "10 minutes ago"The systemd service logs everything through journalctl:
# See the last 50 lines
sudo journalctl -u minecraft -n 50
# Follow logs in real-time
sudo journalctl -u minecraft -f
# Search for errors
sudo journalctl -u minecraft | grep -i errorServer won't start?
- Check logs:
sudo journalctl -u minecraft -n 100 - Verify Java is installed:
java -version - Make sure the minecraft user has permissions:
ls -l /opt/minecraft/server/
Server keeps restarting?
- Check if you have enough RAM
- Look for errors in the logs
- Make sure the server JAR file exists and is named correctly
Want to disable auto-restart?
Edit the service file and change Restart=on-failure to Restart=no, then reload:
sudo systemctl daemon-reload
sudo systemctl restart minecraftThe service file includes optimized G1GC flags for Minecraft. If you're running on lower RAM:
- Reduce
-Xmsand-Xmxvalues - Consider using Fabric with performance mods (see mods guide)
- Monitor RAM usage:
free -horhtop
That's it! Your server should now auto-start on boot and restart if anything goes wrong.