Sunday, October 27, 2024

How to Create Minecraft Server on Raspberry Pi 5

For aspiring Raspberry Pi programmers looking to host their own Minecraft server, getting it to launch automatically upon boot can seem like a daunting task. This guide will walk you through the process, combining the power of screen for interactive control and systemd for seamless background execution.

Understanding the Challenge

The core issue lies in the way screen and systemd interact. screen is a powerful tool for creating persistent terminal sessions, allowing you to run processes even after disconnecting from the Raspberry Pi. systemd, on the other hand, is the system's service manager, responsible for starting and stopping programs at boot.

The challenge arises when using screen within a systemd service. systemd expects services to complete quickly, while screen detaches its process, creating a long-running background session. This leads to systemd marking the service as inactive after screen detaches, effectively killing the server process.

The Solution: Leveraging Systemd's forking Capability

The key is to inform systemd that your service will spawn a child process. By setting the Type=forking option in your systemd service unit, you tell systemd to wait for the child process before marking the service as inactive. This keeps your Minecraft server running in the background, even after the initial screen process detaches.



Step-by-Step Guide

Here's how to launch your Minecraft server with screen and systemd, guaranteeing both background operation and interactive access:

  1. Prepare Your Minecraft Server:

    • Download Forge: Get the required Forge version (1.12.2 in your case) from the official website.

    • Create a .sh Script: This script will simplify starting your server. Save the following code as minecraft.sh in your server directory:

      #!/bin/bash
      screen -S mcserver -dm java -Xms1024M -Xmx2048M -jar /home/pi/MinecraftServer/server/forge-1.12.2-14.23.5.2854.jar nogui
          

    • Make the script executable: Use chmod +x minecraft.sh.

  2. Create Your systemd Unit:

    • Create a new file called mcserver.service in /etc/systemd/system with the following content:

      [Unit]
      Description=Minecraft Server
      
      [Service]
      Type=forking
      ExecStart=/home/pi/MinecraftServer/server/minecraft.sh
      
      [Install]
      WantedBy=multi-user.target
          

  3. Enable and Start the Service:

    • Enable the service for automatic startup at boot:

            systemctl enable mcserver.service
          

    • Start the service immediately:

            systemctl start mcserver.service
          

  4. Verify the Service:

    • Check the service status:

            systemctl status mcserver.service
          

      You should see a message indicating the service is "active (running)" and "Main PID" referencing the screen process.

  5. Accessing the Server:

    • List Running Screens:

            screen -list
          

      You should see a screen session named mcserver.

    • Connect to the Screen:

            screen -r mcserver
          

      You'll be connected to the Minecraft server terminal, ready to run commands.

Additional Considerations

  • Log Files: Consider setting up log files to monitor your server's activity and troubleshoot issues.

  • Firewall: Adjust your firewall rules to allow incoming connections to the Minecraft server port.

  • Security: Implement security measures to protect your server from unauthorized access.

Conclusion

By combining the strengths of screen and systemd, you can launch your Minecraft server effortlessly on boot, while retaining the flexibility to interact with it whenever you need. This approach provides a reliable and convenient way to enjoy your own Minecraft server on your Raspberry Pi, opening up a world of possibilities for both personal and collaborative gameplay.

0 comments:

Post a Comment