Skip to content

Instantly share code, notes, and snippets.

@glektarssza
Last active May 10, 2025 14:00
Show Gist options
  • Select an option

  • Save glektarssza/d82ed64131506c07bc26c37e41ff590b to your computer and use it in GitHub Desktop.

Select an option

Save glektarssza/d82ed64131506c07bc26c37e41ff590b to your computer and use it in GitHub Desktop.
Minecraft systemd files
#-- Put your environment variable overrides here!
[Unit]
#-- Unit metadata
Description=Minecraft Server (%i)
#-- Dependencies
After=network-online.target
Wants=network-online.target
[Service]
#-- Service type
Type=simple
#-- Restart on failure
Restart=on-abnormal
#-- JVM returns exit code 143 on SIGTERM, silly thing
SuccessExitStatus=143
#-- Wait 3 seconds before restarting
RestartSec=3s
#-- Limit to a maximum of 3 restarts per day
StartLimitInterval=1d
StartLimitBurst=3
#-- User and group
User=minecraft
Group=minecraft
#-- Define I/O
[email protected]
StandardInput=socket
StandardOutput=journal
StandardError=journal
#-- Directories
RuntimeDirectory=minecraft
RuntimeDirectoryMode=0775
RuntimeDirectoryPreserve=no
WorkingDirectory=/mnt/minecraft-data/%i
#-- Protect various system parameters
ProtectSystem=full
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
#-- Timeout settings
TimeoutStopSec=70
#-- Default environment variables
Environment=JAVA_ARGS=
Environment=MC_SERVER_JAR=
Environment=MC_ARGS=nogui
Environment=MC_RAM_MIN=8G
Environment=MC_RAM_MAX=8G
Environment=MC_STOP_BEFORE_SAVE_DELAY=10
Environment=MC_STOP_AFTER_SAVE_DELAY=5
Environment=MC_FINAL_STOP_DELAY=3
#-- Environment file
EnvironmentFile=-/mnt/minecraft-data/%i/.envrc
#-- Startup command(s)
ExecStart=/mnt/minecraft-data/%i/startMinecraftServer.sh
#-- Reload command(s)
ExecReload=echo "/reload" > /run/minecraft/%i.stdin
#-- Stop command(s)
ExecStop=/bin/sh -c 'echo "/say Server is preparing to shut down!" > /run/minecraft/%i.stdin'
ExecStop=/bin/sh -c 'echo "/say Saving the world in ${MC_STOP_BEFORE_SAVE_DELAY} seconds..." > /run/minecraft/%i.stdin'
ExecStop=sleep ${MC_STOP_BEFORE_SAVE_DELAY}
ExecStop=/bin/sh -c 'echo "/say Saving the world!" > /run/minecraft/%i.stdin'
ExecStop=/bin/sh -c 'echo "/save-all" > /run/minecraft/%i.stdin'
ExecStop=/bin/sh -c 'echo "/say World saved! Shutting down in ${MC_STOP_AFTER_SAVE_DELAY} seconds..." > /run/minecraft/%i.stdin'
ExecStop=sleep ${MC_STOP_AFTER_SAVE_DELAY}
ExecStop=/bin/sh -c 'echo "/say Shutting down! Good bye!" > /run/minecraft/%i.stdin'
ExecStop=sleep ${MC_FINAL_STOP_DELAY}
ExecStop=/bin/sh -c 'echo "/stop" > /run/minecraft/%i.stdin'
[Install]
WantedBy=multi-user.target
[Unit]
[email protected]
[Socket]
#-- User and group
SocketUser=minecraft
SocketGroup=minecraft
#-- The mode to create the FIFO node in
SocketMode=0665
#-- Define standard input socket
ListenFIFO=%t/minecraft/%i.stdin
#-- Remove when the parent service stops
RemoveOnStop=true
[Unit]
#-- Unit metadata
Description=Restart the %i Minecraft server daily.
#-- Dependencies
Requisite=minecraft@%i.service
[Service]
#-- Service type
Type=oneshot
#-- Timeout settings
TimeoutStartSec=90
#-- Startup command(s)
ExecStart=/usr/bin/systemctl try-restart minecraft@%i.service
[Unit]
#-- Unit metadata
Description=Restart the %i Minecraft server daily.
[Timer]
#-- The calendar time to restart at (optionally add a time zone)
OnCalendar=*-*-* 00:00:00
#-- Persist next restart time to disk
Persistent=true
[Install]
WantedBy=timers.target
#!/usr/bin/env bash
exec java -Xms${MC_RAM_MIN} -Xmx${MC_RAM_MAX} ${JAVA_ARGS} -jar "$(find . -maxdepth 1 -iname "${MC_SERVER_JAR}" | head -n1)" ${MC_ARGS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment