Skip to content

Instantly share code, notes, and snippets.

@CodeCorrupt
Last active February 5, 2023 06:29
Show Gist options
  • Save CodeCorrupt/232b5ba023c18790fa7155d6bd351c77 to your computer and use it in GitHub Desktop.
Save CodeCorrupt/232b5ba023c18790fa7155d6bd351c77 to your computer and use it in GitHub Desktop.
Run Minecraft server as Systemd service (with support for commands) the right way

This is a modification to the setup recommended by the minecraft wiki at https://minecraft.fandom.com/wiki/Tutorials/Server_startup_script. Start by following the directions there, then modify to match what's here.

This allows control over stdin of the server by piping commands to %i/%v/mc.stdin. ie: echo "say Hello" > /run/survival/mc.stdin %i is your systems runtime directory (mine is /run %v is your servers name, explained in the wiki linked

[Unit]
Description=Minecraft Server %i
After=network.target
[Service]
WorkingDirectory=/opt/minecraft/%i/
# Solves the issue where the minecraft server will endlessly restart itself
# See https://askubuntu.com/questions/953920/systemctl-service-timed-out-during-start for more info
Type=simple
ProtectSystem=full
# Read only mapping of /usr /boot and /etc
ProtectKernelTunables=true
# /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-only within the unit. It is recommended to turn this on for most services.
# Implies MountFlags=slave
ProtectKernelModules=true
# Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special file systems or extra kernel modules to work
# Implies NoNewPrivileges=yes
ProtectControlGroups=true
# It is hence recommended to turn this on for most services.
# Implies MountAPIVFS=yes
# Set default memory values
Environment="MCMINMEM=512M" "MCMAXMEM=1024M" "SHUTDOWN_DELAY=5" "POST_SHUTDOWN_DELAY=10"
# Change memory values in environment file
EnvironmentFile=-/opt/minecraft/%i/server.conf
Sockets=minecraft@.socket
StandardInput=socket
StandardOutput=journal
StandardError=journal
ExecStartPre=/bin/sh -c 'set -x; chown -R minecraft:minecraft /opt/minecraft/%i'
ExecStart=/bin/sh -c \
'JAR_FILE=$(find -L . \
-maxdepth 1 \
-type f \
-iregex ".*/\\(FTBServer\\|craftbukkit\\|spigot\\|paper\\|forge\\|minecraft_server\\).*jar" \
-print0 \
-quit); \
sudo -u minecraft /usr/lib/jvm/java-8-openjdk-amd64/bin/java \
-server \
-Xms${MCMINMEM} \
-Xmx${MCMAXMEM} \
-XX:+UseG1GC \
-XX:ParallelGCThreads=2 \
-XX:MinHeapFreeRatio=5 \
-XX:MaxHeapFreeRatio=10 \
-jar $JAR_FILE \
--nogui'
ExecReload=/bin/sh -c \
'echo "reload" > /%t/%i/mc.stdin'
ExecStop=/bin/sh -c \
'echo "say SERVER SHUTTING DOWN IN ${SHUTDOWN_DELAY} SECONDS. Saving map..." > /%t/%i/mc.stdin'
ExecStop=/bin/sh -c \
'/bin/sleep ${SHUTDOWN_DELAY}'
ExecStop=/bin/sh -c \
'echo "save-all" > /%t/%i/mc.stdin'
ExecStop=/bin/sh -c \
'echo "stop" > /%t/%i/mc.stdin'
ExecStop=/bin/sh -c \
'/bin/sleep ${POST_SHUTDOWN_DELAY}'
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
#########
# HowTo
#########
#
# Create a directory in /opt/minecraft/XX where XX is a name like 'survival'
# Add minecraft_server.jar into dir with other conf files for minecraft server
#
# Enable/Start systemd service
# systemctl enable minecraft@survival
# systemctl start minecraft@survival
#
# To run multiple servers simply create a new dir structure and enable/start it
# systemctl enable minecraft@creative
# systemctl start minecraft@creative
#
# To change specific server memory assignment, create file /opt/minecraft/XX/server.conf (where XX is your server name) and add below lines:
# MCMINMEM=512M
# MCMAXMEM=2048M
[Socket]
ListenFIFO=%t/%i/mc.stdin
Service=minecraft@.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment