Skip to main content

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:

  1. The systemd minecraft@ instance specific service launches screen. (ie: systemctl start minecraft@creative1)minecraft@creative1)
  2. screen launches the script start-server-in-screen-systemdminecraft-server  (as instructed by the SystemD service).
  3. start-server-in-screen-systemdminecraft-server launches "java fabric-server-launch.jar" (with a lot of other arguments for optimization)
  4. java loads fabric-server-launch.jar
  5. fabric-server-launch.jar takes care of loading your Minecraft server instance
  6. 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:

  1. 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.
  2. 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.

# create minecraft systemd service and socket files
# copy contents of files from contents further down the page
vi /etc/systemd/system/minecraft@.service
vi /etc/systemd/system/minecraft@.socket

# example for server-instance creative1

# required files for server-instance creative1
# copy contents of file from contents further down the page
vi /opt/minecraft/server-instances/creative1/start-server-in-screen-systemd

# systemctl commands for server-instance creative1
systemctl enable minecraft@creative1.service
systemctl start minecraft@creative1.service
systemctl status minecraft@creative1.service
Prep commands
MCROOT=/opt/minecraft# create base folder
mkdir -p $MCROOT//opt/minecraft/server-instances

# create specific instance folder
INSTANCENAME=creative1
mkdir -p /opt/minecraft/server-instances/$MCROOT/toolsINSTANCENAME

# change ownership if desired
chown -R minecraftuser:minecraftuser $MCROOT/server-instances

# gitcreate the systemd minecraft service runner
touch /etc/systemd/system/minecraft@.service
# copy the script contents from below into the file and makesave
mcrconvi cd $MCROOT/tools
git clone https://github.com/Tiiffi/mcrcon.git
cd mcrcon
makeetc/systemd/system/minecraft@.service

# only install it if you want, otherwise just runreload the binarysystemd daemon
systemctl daemon-reload

# example: creative1 instance
touch /opt/minecraft/server-instances/creative1/start-minecraft-server
# copy the script contents from herebelow #into sudothe makefile installand #save
createvi server instance folders
MCINSTANCE1=$MCROOT//opt/minecraft/server-instances/creative1
mkdir -p $MCINSTANCE1
chown -R minecraftuser:minecraftuser $MCINSTANCE1creative1/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]
WorkingDirectory=/opt/minecraft/server-instances/%iType=forking

User=minecraftuser
Group=minecraftuser

Sockets=minecraft@%i.socket
StandardInput=socket
StandardOutput=journal
StandardError=journal

PrivateUsers=true
ProtectSystem=full
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
NoNewPrivileges=true

WorkingDirectory=/opt/minecraft/server-instances/%i

Type=simple
ExecStart=/usr/bin/screen -DdmS -mminecraft-%i ./start-minecraft-server
ExecStop=/usr/bin/screen -S minecraft-%i ./start-server-in-screen-systemd
ExecStop=/bin/sh -c "/bin/echoX stop > /opt/minecraft/server-instances/%i/systemd.stdin"

Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target
/etc/systemd/system/minecraft@.socket

 

[Unit]
BindsTo=minecraft@%i.service

[Socket]
ListenFIFO=/opt/minecraft/server-instances/%i/systemd.stdin
Service=minecraft@%i.service
SocketUser=minecraftuser
SocketGroup=minecraftuser
RemoveOnStop=true
SocketMode=0600

/opt/minecraft/server-instances/%i/start-server-in-screen-systemdminecraft-server

The systemd service launches screen and tells it to run this script file per instance, which actually runs SystemD Minecraft@ Service -> Screen -> start-server-in-screen-systemd -> Java -> Fabric -> Minecraftinstance.

#!/bin/bash

# Simplesuper basic command toline start fabric server with diffent RAM optionsexample
#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

# LaunchingCommand Minecraftline withbased aon specificaikars java binary using Aikar's server flagssuggested:
/usr/lib/opt/jvm/java-17-oracle/temurin/jdk-21/bin/java -Xms8GXms8192M -Xmx8GXmx8192M --add-modules=jdk.incubator.vector -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -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 -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