Skip to content

Instantly share code, notes, and snippets.

@stupidpupil
Last active February 6, 2024 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stupidpupil/3b984bd7d7eed6543ac06089d4deb984 to your computer and use it in GitHub Desktop.
Save stupidpupil/3b984bd7d7eed6543ac06089d4deb984 to your computer and use it in GitHub Desktop.
Yet Another Minecraft systemd service file, and accessories. (Gracefully handles shutdown properly, whilst still eventually SIGKILL-ing if that doesn't work.)
#!/bin/sh
server_properties_path=$(dirname "$0")"/../server/server.properties"
rcon_port=$(awk -F "=" '/rcon.port/ {print $2}' "$server_properties_path")
rcon_password=$(awk -F "=" '/rcon.password/ {print $2}' "$server_properties_path")
"$(dirname "$0")/mcrcon" -H 127.0.0.1 -P "$rcon_port" -p "$rcon_password" "$1"
[Unit]
Description=Save Minecraft server worlds
Requisite=minecraft.service
After=minecraft.service
[Service]
PrivateUsers=yes
User=minecraft
Group=minecraft
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
CapabilityBoundingSet=
NoNewPrivileges=yes
Type=oneshot
ExecStart=/mnt/puddle/minecraft/mcrcon/mcrcon-wrapper save-all
SuccessExitStatus=0 1
[Unit]
Description=Autosave Minecraft worlds
Requisite=minecraft.service
After=minecraft.service
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=false
[Install]
WantedBy=minecraft.service
[Unit]
Description=Minecraft Server
Requires=network.target
After=network.target
[Service]
WorkingDirectory=/mnt/puddle/minecraft/server
ReadWritePaths=/mnt/puddle/minecraft/server
ReadOnlyPaths=/mnt/puddle/minecraft/spigotbuild /mnt/puddle/minecraft/mcrcon
PrivateUsers=yes
User=minecraft
Group=minecraft
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
CapabilityBoundingSet=
NoNewPrivileges=yes
ExecStart=/usr/bin/java -Xmx2G -Xms1G -jar server.jar
ExecStop=/mnt/puddle/minecraft/mcrcon/mcrcon-wrapper stop
# mcron-wrapper provides hostname and password for mcrcon
KillMode=mixed # Otherwise it tries to signal subprocesses straightaway
KillSignal=SIGCHLD # SIGCHLD should just be ignored
SuccessExitStatus=0 1
# But 10 minutes later, it'll SIGKILL everything if sending 'stop' didn't work
TimeoutStopSec=600
@Pavel-Matskevich
Copy link

KillSignal=SIGCHLD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment