SystemD
The two files below are the merger of other Minecraft SystemD service files I came across on the web and the screen-based method I originally used for running Minecraft instances.
Here's a breakdown of the processes that facilitate automatically loading the Minecraft server instance(s) when the system boots up:
- The systemd minecraft@ instance specific service launches screen. (ie:
systemctl start minecraft@creative1) - screen launches the script start-minecraft-server (as instructed by the SystemD service).
- start-minecraft-server launches
java fabric-server-launch.jar(with a lot of other arguments for optimization) - java loads fabric-server-launch.jar
- fabric-server-launch.jar takes care of loading your Minecraft server instance
- You can access your screened server with
screen -rd minecraft-creative1(or whatever name you used)
I chose to use screen a bash script instead of launching java directly from systemd for these two reasons:
- screen allows you to directly interact with the Minecraft server console without needing an rcon application, and its the way I'm used to dealing with server instances.
- Using a bash script to launch the Minecraft server instance was easier to deal with per-instance customizations. You can facilitate the same thing directly from systemd, I just didn't want to take the time to learn how to do.
Setting up your environment
Below is an example of the files and folders that are needed for this setup.
Prep commands
# create base folder
mkdir -p /opt/minecraft/server-instances
# create specific instance folder
INSTANCENAME=creative1
mkdir -p /opt/minecraft/server-instances/$INSTANCENAME
# change ownership if desired
chown -R minecraftuser:minecraftuser $MCROOT/server-instances
# create the systemd minecraft service runner
touch /etc/systemd/system/minecraft@.service
# copy the script contents from below into the file and save
vi /etc/systemd/system/minecraft@.service
# reload the systemd daemon
systemctl daemon-reload
# example: creative1 instance
touch /opt/minecraft/server-instances/creative1/start-minecraft-server
# copy the script contents from below into the file and save
vi /opt/minecraft/server-instances/creative1/start-minecraft-server
/etc/systemd/system/minecraft@.service
[Unit]
Description=Minecraft Server in a Screen - %i
Wants=network-online.target
After=network-online.target
ConditionPathExists=/opt/minecraft/server-instances/%i
[Service]
Type=forking
User=minecraftuser
Group=minecraftuser
WorkingDirectory=/opt/minecraft/server-instances/%i
ExecStart=/usr/bin/screen -dmS minecraft-%i ./start-minecraft-server
ExecStop=/usr/bin/screen -S minecraft-%i -X stop
Restart=on-failure
RestartSec=60s
[Install]
WantedBy=multi-user.target
/opt/minecraft/server-instances/%i/start-minecraft-server
The systemd service launches screen and tells it to run this script file per instance.
#!/bin/bash
# super basic command line example
#java -Xms4096M -Xmx4096M -jar fabric-server-launch.jar nogui
#java -Xms6144M -Xmx6144M -jar fabric-server-launch.jar nogui
# 20211210
# Added the following line to fix a security bug announced today
# -Dlog4j2.formatMsgNoLookups=true
# Command line based on aikars suggested:
/opt/jvm/temurin/jdk-21/bin/java -Xms8192M -Xmx8192M --add-modules=jdk.incubator.vector -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -jar fabric-server-launch.jar --nogui
SystemD systemctl commands
# systemctl commands for server-instance creative1
systemctl enable minecraft@creative1.service
systemctl start minecraft@creative1.service
systemctl status minecraft@creative1.service
systemctl stop minecraft@creative1.service
screen commands
# list all screens
screen -list
# join screen for instance creative1
screen -rd minecraft-creative1
#end