So this is my setup for a minecraft server, it isn't the best, but it works well for me and my friends.
So I mostly start with setting up a Debian Server with a ucer called minecraft or mc, or something similar.
Then I create the following files in the /etc/systemd/system/
directory:
minecraft.service
:
[Unit]
Description=Minecraft Server
[Service]
Type=simple
WorkingDirectory=/home/mc/server/
#ExecStart=/usr/bin/java -Xmx6G -jar server.jar nogui
ExecStart=/usr/bin/java -Xms6G -Xmx6G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar server.jar nogui
User=mc
Restart=on-failure
Sockets=minecraft.socket
StandardInput=socket
StandardOutput=journal
StandardError=journal
ExecStop=/bin/bash -c 'echo "/say Server is closing in 10 seconds!" > /run/minecraft.stdin && sleep 10;'
[Install]
WantedBy=multi-user.target
You will have to alter this file to set the maximum and minimum amount of ram your java vm should use, as well as the WorkingDirectory, User, etc. to fit your server.
or you can generate new flags using this website: https://flags.sh/
Then I create the socket file to communicate with the standard in of the server:
minecraft.socket
[Unit]
PartOf=minecraft.service
[Socket]
ListenFIFO=%t/minecraft.stdin
I then enable it using systemctl enable minecraft.service
You can then either restart your vps or just run this: systemctl start minecraft.service
To interface with the console I open up a tmux session with two panes, in
one I run journalctl -u minecraft -f
which just follows the log of the minecraft.service
and on the second pane I make commands like so:
echo "help" > /run/minecraft.stdin
or you can use this script:
#!/usr/bin/env bash
echo "Exit with ^C \n\n"
while true; do
echo -n "[Minecraft Console] "
read input
echo "$input" > /run/minecraft.stdin && lastDate=$(($(date +%s) - 1))
sleep 1
journalctl --no-pager --since @"$lastDate"
lastdate=$(date +%s)
done
This script isn't perfect because I am not familiar enough with systemd to make it work perfectly. The tmux approach is better for now until I update this script. Using timestamps like I do here is just sloppy.