Skip to content

Instantly share code, notes, and snippets.

@Leopere
Forked from nathanielc/gist:9b98350ccbcbf21256d7
Last active November 26, 2020 20:13
Show Gist options
  • Save Leopere/ee6f5c5823cadec09db1 to your computer and use it in GitHub Desktop.
Save Leopere/ee6f5c5823cadec09db1 to your computer and use it in GitHub Desktop.
Systemd unit for minecraft-spigot server
#!/bin/bash
#
# Settings file for minecraft-init
# ================================
#
# Make a copy of this file named config
# and edit the variables to your needs.
#
### ChamCode
# Name of this server instance
SERVERNAME="_SNAME_"
# Server Port
PORT="SERV_PORT"
# Name of CB_JAR.jar
CB_JAR="spigot.jar"
# Define the release of CraftBukkit to use (stable or unstable)
CB_RELEASE="stable"
# Name of server.jar to use (either $MC_JAR or $CB_JAR)
SERVICE=$CB_JAR
# Name to use for the screen instance
SCREEN="mc${SERVERNAME}"
# User that should run the server
USERNAME="${SERVERNAME}"
# Path to minecraft server directory
MCPATH="/opt/minecraft/${USERNAME}"
# Where the worlds are located on the disk. Can not be the same as MCPATH.
# You need to move your worlds to this directory manually, the script
# will then handle the nessessay symlinks.
WORLDSTORAGE="${MCPATH}/worlds"
# Number of CPUs/cores to use
CPU_COUNT=2
# Initial memory usage
INITMEM="4G"
# Maximum amount of memory to use
# Remember: give the ramdisk enough space, subtract from the total amount
# of RAM available the size of your map and the RAM-consumption of your base system.
MAXMEM="4G"
# Whether to output commands for the plugin ConsoleLikeChat ($1 is the command to run)
FORMAT='$1'
# Settings for backups
# ===============================
# Location for world backups
BACKUPPATH="${MCPATH}/backups/snapshots"
# Where the whole minecraft directory is copied when whole-backup is executed
# whole-backup is a complete uncompressed backup of the whole server folder.
WHOLEBACKUP="_SBACKUP_/wholebackup/${SERVERNAME}"
# Format for world backup (tar or zip).
BACKUPFORMAT="zip"
# Normally backups will be put in a subfolder to $BACKUPPATH with todays date
# and the backups themselves will have a timestamp.
# But if BACKUPSCRIPTCOMPATIBLE is set the world backups will be put directly
# in $BACKUPPATH without timestamp to be compatible with
# [backup rotation script](https://github.com/adamfeuer/rotate-backups)
#
BACKUPSCRIPTCOMPATIBLE=YES
# If WORLDEDITCOMPATIBLE is set the world backups will be created compatible to WorldEdit
# in $BACKUPPATH as WORLD_NAME/DATE.(tar.bz2|zip) with the requested directory structure
#
# WORLDEDITCOMPATIBLE=YES
# Compress the whole backup with bzip2?
# Note that this may not save a lot of disk space since there can be a lot of files
# in your server directory, that are already compressed, but it can slow down the
# backup a bit. This highly depends on the plugins you're using.
#
# For example: The png files generated by Dynmap are already compressed and still use
# a lot of space in your server directory, so the compression ratio of the compressed
# backup will not be very high.
COMPRESS_WHOLEBACKUP=YES
# Settings for log rolling
# ===============================
# Location for old logs
# Used by the log-roll command
LOGPATH="${MCPATH}/backup/logs"
# Whether or not to gzip logs (must be commented out for no - DO NOT CHANGE TO NO)
#
GZIPLOGS=YES
# What to append to the logfile name (Leave blank for nothing)
LOGFILEAPPEND="logfile_"
# Settings for overviewer command
# ===============================
# Where the Map is generated
OUTPUTMAP="/home/${USERNAME}/mc-overviewer/render"
# Path to Minecraft-Overviewer
OVPATH="/home/${USERNAME}/mc-overviewer/Minecraft-Overviewer"
# Path for the config file of Overviewer
OVCONFIGPATH="${MCPATH}/mc-overviewer"
# Name of Overviewer config file
OVCONFIGNAME="config.py"
# Path for backup worlds
#OVBACKUP="/home/${USERNAME}/mc-overviewer/overviewerbackups"
# Things to leave alone ;)
# =====================
# =====================
INVOCATION="java -Xmx$MAXMEM -Xincgc -XX:PermSize=64m -XX:MaxPermSize=256m -XX:ParallelGCThreads=$CPU_COUNT -Dcom.mojang.eula.agree=true -Dfile.encoding=UTF-8 -jar $SERVICE --port $PORT --online-mode true nogui"
# Path to the the mounted ramdisk (the default will work in most senarios).
RAMDISK="/dev/shm"
[Unit]
Description=Minecraft Server %i
Documentation=https://gist.github.com/chamunks/ee6f5c5823cadec09db1
User=%i
Group=%i
Nice=0
EnvironmentFile=/etc/minecraft/mc%i.conf
#Runlevel=3 # Probably Redundant since the "wanted=" directive below.
[Service]
Type=oneshot
WorkingDirectory=/opt/minecraft-%i
#User=mc%i
User=minecraft
ExecStartPre=fuser -k -n tcp $PORT
ExecStart=/usr/bin/screen -DmS mc-%i /bin/java -Xmx$MAXMEM -Xincgc -XX:PermSize=64m -XX:MaxPermSize=256m -XX:ParallelGCThreads=2 -Dcom.mojang.eula.agree=true -Dfile.encoding=UTF-8 -jar spigot.jar --port $PORT --online-mode true nogui
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\\015'
ExecStop=/bin/sleep 2
[Install]
WantedBy=multi-user.target
# First Attempt at a systemd unit file #
# Research and References:
# http://www.freedesktop.org/software/systemd/man/systemd.unit.html
# http://www.freedesktop.org/software/systemd/man/systemd.directives.html
# http://www.freedesktop.org/software/systemd/man/systemctl.html
# https://wiki.archlinux.org/index.php/systemd#Using_units
# https://coreos.com/docs/launching-containers/launching/getting-started-with-systemd/
# https://coreos.com/docs/quickstart/
# http://www.freedesktop.org/software/systemd/man/systemctl.html
## Q&A
# https://gist.github.com/chamunks/fd2fe1aa66b4337bb346
## Other system.d units and suggestions
# http://www.petersplanet.nl/2014/11/use-systemd-to-start-and-stop-your.html
## Source
# https://gist.github.com/nvcook42/9b98350ccbcbf21256d7
# https://github.com/Ahtenus/minecraft-init # config I used to use.
#########
# HowTo
#########
#
# Create 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment