LochStudios  /  Help Centre  /  Game Servers  /  Run a Minecraft server as a systemd service (auto-start)

Run a Minecraft server as a systemd service (auto-start)

Create a systemd unit file so your Minecraft server starts automatically on reboot and runs as a managed service.

Updated

Overview

Running your Minecraft server as a systemd service means it will start automatically when your VPS reboots, and you can easily start, stop, and monitor it using standard Linux service commands.

Prerequisites

  • Minecraft server already installed (see Set up a Minecraft Java Edition server on Linux)
  • A dedicated minecraft user (created during initial setup)
  • Root or sudo access

Steps

Step 1: Create the systemd service file

sudo nano /etc/systemd/system/minecraft.service

Paste the following content:

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

[Service]
Type=simple
User=minecraft
WorkingDirectory=/home/minecraft/server
ExecStart=/usr/bin/java -Xmx2048M -Xms2048M -jar server.jar nogui
Restart=on-failure
RestartSec=30
StandardOutput=journal
StandardError=journal
StandardInput=socket

[Install]
WantedBy=multi-user.target

Key settings:
- User=minecraft: Runs the service as the minecraft user (safer than root)
- WorkingDirectory: Where the server files and world data are stored
- ExecStart: The Java command to launch the server (adjust -Xmx and -Xms to your server RAM)
- Restart=on-failure: Automatically restart if the server crashes
- RestartSec=30: Wait 30 seconds before restarting

Save the file (Ctrl+O, Enter, Ctrl+X in nano).

Step 2: Reload systemd and enable the service

sudo systemctl daemon-reload
sudo systemctl enable minecraft

Step 3: Start the server

sudo systemctl start minecraft

Step 4: Verify the service is running

sudo systemctl status minecraft

You should see "active (running)" in the output. If it shows "failed", check the logs:

sudo journalctl -u minecraft -n 50

This displays the last 50 lines of the service log.

Managing the Service

Stop the server gracefully:

sudo systemctl stop minecraft

Restart the server:

sudo systemctl restart minecraft

Check real-time logs:

sudo journalctl -u minecraft -f

Press Ctrl+C to exit the log view.

Check if it's set to auto-start on reboot:

sudo systemctl is-enabled minecraft

Should output "enabled".

Disable auto-start (if needed):

sudo systemctl disable minecraft

Troubleshooting

Service fails to start:
- Check the error: sudo journalctl -u minecraft -n 50
- Verify the jar file path exists: ls -l /home/minecraft/server/server.jar
- Ensure the minecraft user owns the directory: sudo chown -R minecraft:minecraft /home/minecraft/server

Server keeps crashing:
- Check available RAM: free -h. If memory is low, reduce -Xmx in the service file.
- Review the world for errors: sudo journalctl -u minecraft -n 100

Can't connect after reboot:
- Verify the service started: sudo systemctl status minecraft
- Check the firewall is still open: sudo ufw status

Tips

  • RAM allocation: Set -Xmx to 70% of your server's available RAM. For a 4 GB VPS, use -Xmx2816M.
  • Edit the service: After any change to the service file, reload with sudo systemctl daemon-reload before restarting.
  • Multiple servers: You can create multiple service files (e.g., minecraft-1.service, minecraft-2.service) with different ports and directories.
  • Backups: Set up a cron job to periodically backup the world directory. Use World Guard or mod-based backups for in-game safety.

Next Steps

  • Run a game server persistently with screen or tmux for manual server management
  • Add plugins or mods to expand your server's features

Was this article helpful?

← Back to Game Servers