How to Reliably Run a Minecraft Server on OCI Using Systemd for Auto-Restarts
In this guide, we will build on the previous tutorial for setting up a modded Minecraft server on Oracle Cloud Infrastructure (OCI). You will learn how to configure your Minecraft server to run as a systemd service, ensuring it starts automatically at boot and restarts in case of crashes.
Step 1: Verify Your Server Setup
Before proceeding, ensure that:
- You have completed the basic setup of a Minecraft server on OCI as outlined in the previous guide.
- The Minecraft server files are located in
~/minecraft
and can be started with the command:
Replacejava -Xmx8G -Xms8G -jar forge-*.jar nogui
8G
with your desired RAM allocation.
Step 2: Create a Systemd Service File
-
Open a terminal and switch to the root user:
sudo su
-
Navigate to the
systemd
configuration directory:cd /etc/systemd/system
-
Create a new service file for your Minecraft server:
nano minecraft.service
-
Add the following configuration to the file:
[Unit] Description=Minecraft Server After=network.target [Service] User=ubuntu WorkingDirectory=/home/ubuntu/minecraft ExecStart=/usr/bin/java -Xmx8G -Xms8G -jar forge-*.jar nogui Restart=always RestartSec=10 StandardOutput=append:/home/ubuntu/minecraft/server.log StandardError=append:/home/ubuntu/minecraft/server.log [Install] WantedBy=multi-user.target
- Replace
ubuntu
with the username of the user running the server. - Adjust the
-Xmx
and-Xms
values to match your server's resources.
- Replace
-
Save and exit the file by pressing
Ctrl+O
,Enter
, andCtrl+X
.
Step 3: Reload Systemd and Enable the Service
- Reload the systemd daemon to recognize the new service:
systemctl daemon-reload
- Enable the Minecraft service to start at boot:
systemctl enable minecraft.service
- Start the Minecraft service:
systemctl start minecraft.service
- Check the service status to ensure it is running:
You should see output indicating that the service is active and running.systemctl status minecraft.service
Step 4: Configure Firewall for Automatic Restarts
If you have not already configured the firewall for port 25565 (Minecraft’s default port), ensure the rule is set in your OCI Console:
- Go to Networking > Virtual Cloud Networks in the OCI Console.
- Select the VCN and then the Subnet.
- Under Security Lists, confirm that an ingress rule exists for:
- Source CIDR:
0.0.0.0/0
- Destination Port Range:
25565
- Protocol: TCP
- Source CIDR:
Step 5: Test the Service
- Stop the service to simulate a crash:
systemctl stop minecraft.service
- Wait for a few seconds and verify that the service restarts automatically:
systemctl status minecraft.service
- You can also check the log file for restart activity:
tail -f /home/ubuntu/minecraft/server.log
Step 6: Update and Manage Your Service
-
To Restart the Service Manually:
systemctl restart minecraft.service
-
To Disable the Service from Starting at Boot:
systemctl disable minecraft.service
-
To Edit the Service File:
nano /etc/systemd/system/minecraft.service
After editing, always reload the systemd daemon:
systemctl daemon-reload
-
To View Logs:
journalctl -u minecraft.service
Now your Minecraft server is configured to run as a systemd service. It will automatically start on boot, restart after crashes, and log its output to server.log
for easy debugging.
Comments
Post a Comment