Skip to content

Latest commit

 

History

History
146 lines (104 loc) · 3.92 KB

File metadata and controls

146 lines (104 loc) · 3.92 KB

Auto-Restart Setup with systemd

Setting up your Minecraft server to auto-start on boot and restart if it crashes is pretty straightforward with systemd.

Table of Contents

Why Use systemd?

  • Auto-starts when your Pi boots up
  • Automatically restarts if the server crashes
  • Easy to manage with simple commands
  • Proper logging through journalctl

Setup Steps

1. Create a Minecraft User

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/server

2. Create the Service File

Copy 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.target

Note: Adjust the -Xms2G -Xmx5G values based on your Pi's RAM:

  • Pi with 4GB RAM: -Xms1G -Xmx3G
  • Pi with 8GB RAM: -Xms2G -Xmx5G

3. Enable and Start the Service

# 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

Managing Your Server

Common Commands

# 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"

Checking Logs

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 error

Troubleshooting

Server 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 minecraft

Performance Tips

The service file includes optimized G1GC flags for Minecraft. If you're running on lower RAM:

  1. Reduce -Xms and -Xmx values
  2. Consider using Fabric with performance mods (see mods guide)
  3. Monitor RAM usage: free -h or htop

That's it! Your server should now auto-start on boot and restart if anything goes wrong.