|
[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 |
|
|
|
[email protected] |
|
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 |