Daemonizing and World Backups – Improved!

Since my previous post on this subject, I’ve refreshed both of the Minecraft servers and made them directly accessible with the appropriate client. As part of the refresh to Minecraft 1.17 Caves & Cliffs Part 1, and Roguelike Adventures and Dungeons 1.45, I’ve reworked the way the services and backups are configured. There are a lot of improvements to make startup, shutdown, and world backups run a lot smoother and more transparent to the users. The specific code and files are available on Github at this link.

Daemonizing – Running Minecraft as a Service

So far I’ve found the best way to run Minecraft is inside a screen session. This allows to attach/detach the Minecraft console to quickly execute commands without logging into the game client, and also makes it possible to easily pass commands into the screen session. To start Minecraft, I am using Aikar’s flags mostly when calling Java for optimal garbage collection. I also found some inspiration from Justinjahn’s Github on a similar design.

I’ve customized the minecraft.service file, which is available on Github at this link. The screen name is always “mcs” so my other scripts are set to call that session name.

ExecStart=screen -DmS mcs java -Xms10G -Xmx10G -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:MaxGCPauseMillis=100 -XX:+DisableExplicitGC -XX:TargetSurvivorRatio=90 -XX:G1NewSizePercent=50 -XX:G1MaxNewSizePercent=80 -XX:G1MixedGCLiveThresholdPercent=35 -XX:+AlwaysPreTouch -XX:+ParallelRefProcEnabled -jar server.jar nogui
ExecStop=/usr/bin/screen -p 0 -S mcs -X stuff '/say "SERVER SHUTTING DOWN IN 15 SECONDS"\n'
ExecStop=/usr/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mcs -X stuff '/say "SERVER SHUTTING DOWN IN 10 SECONDS"\n'
ExecStop=/usr/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mcs -X stuff '/say "SERVER SHUTTING DOWN IN 5 SECONDS"\n'
ExecStop=/usr/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mcs -X stuff '/save-all flush\n'
ExecStop=/usr/bin/screen -p 0 -S mcs -X stuff '/stop\n'

I experimented with using shell scripts for ExecStart and ExecStop, however I found the script wouldn’t have time to finish properly before the process was killed. Building all of the ExecStop steps into the minecraft.service file did work however. but changing KillSignal=SIGCONT was needed so the service would have time to shut down by performing all of the steps, but not hang the machine on a system shutdown.

Sending Commands Into Screen

From some of the research I found, there was a better way of injecting a command into the Minecraft console. In my previous scripts, I would reattach the screen, but isn’t necessary. Instead the following syntax can be used:

screen -p 0 -S mcs -X stuff 'minecraft command'

This will connect on Window 0, screen session name “mcs”, and then execute the command in quotes.

Backing Up World Files

As I am running the Minecraft servers on premise now instead of Google Cloud, I have a network share mounted for each Minecraft server at /media/backups that connects to a NAS using CIFS. This server will be upgraded soon, but in the meantime I am forced to use CIVS v1.0. This is the line in /etc/fstab that mounts the share:

//x.x.x.x/minecraft/minecraft-caves /media/backups cifs vers=1.0,credentials=/root/.smbcredentials,uid=1000,gid=1000,iocharset=utf8 0 0

This example mounts the backup folder for minecraft-caves to /media/backups, and gets the usernamd and password from /root/.smbcredentials. It ensures the files copied have the proper permissions.

The script itself has also been improved. The script now alerts users on the server that a backup is running, so they will understand if there is a bit of lag during the backup process. The script now checks if the Minecraft service is running before attempting a backup, since half of the commands will fail if the service isnt’ running and the Minecraft screen console isn’t present.

Automation and Cleaning Up

The crontab is set to run two commands every 4 hours. It will run the backup script, and also check for any backup files older than 14 days and delete them.

0 */4 * * * /opt/minecraft/MC-Backup.sh
0 */4 * * * find /media/backups/* -mtime +13 -type f -delete


Leave a Reply

Your email address will not be published. Required fields are marked *