Automating the Minecraft update process

My kid recently got me into Minecraft. We were having issues trying to join games so I did what any tech-savvy parent would do; I installed Minecraft on my home server so that we can easily play together from our LAN. This worked wonderfully until we started getting errors, “Failed to connect to the server. Outdated Server!” This led me on a long journey, trying to remember where I downloaded the Minecraft edition for Linux from and where this was installed on the server. After what felt like an hour, Minecraft was updated and we were finally able to connect and play together. We played without any issues for a few weeks until we got the dreaded “Outdated Server!” error again. It was time to automate the update process.

One big problem that I encountered when automating the update is that Minecraft is really good at making the update process difficult. Navigating to their Downloads page, you’ll see what I’m talking about. They have two editions; Java and Bedrock, and these are both on separate pages that you can’t find on a sitemap. Try finding their Bedrock edition for Linux on their website, it’s impossible. I had to search on Google specifically for, “Minecraft Bedrock Linux Server Download” to find the page where I can download my updated version of Minecraft. Now that I found the page, I can proceed to create the update.sh file and build a script to fit my needs:

#!/bin/bash
 cd updateMinecraft
 wget -nv -O- https://www.minecraft.net/en-us/download/server/bedrock | grep -o '[^"[:space:]]://[^"[:space:]]bin-linux[^"[:space:]]*.zip' | head -n5 | while read x; do
 wget -nv "$x"
 unzip -o *.zip
 rm *.zip
 cp perms.txt permissions.json
 sed -i -e 's/allow-cheats=false/allow-cheats=true/g' /home/dumbledore/updatedMinecraft/server.properties
 chmod +x bedrock_server
 done 

This script currently runs from a separate directory named updateMinecraft. I chose to run this from a separate directory to ensure that this doesn’t touch the production directory. I’ll likely update this to update the production directory in the future after everything is tidied up. Let’s now look at what the Bash script is doing.

#!/bin/bash

#! (shebang) is telling the operating system which interpreter to use for the script. This is a Bash script so we state that at the top of the file.

 cd updateMinecraft

cd will change our directory, ensuring that the script runs from the specified folder on the server.

 wget -nv -O- https://www.minecraft.net/en-us/download/server/bedrock | grep -o '[^"[:space:]]://[^"[:space:]]bin-linux[^"[:space:]]*.zip' | head -n5 | while read x; do
 wget -nv "$x"

Here is where is starts to get a little tricky. wget is used to download files from the command line interface. I’m using the -nv and -O- flags to set wget to non verbose and to save the output to a file. So we’re downloading the Bedrock server download page, then piping that to grep (search) for a zip file with bin-linux in the file name. The file name always changes with each update so searching for the bin-linux string should do the trick for future updates. This is also required because there are two zip files on the Bedrock server download page, one for Windows and one for Linux. Using Grep to find the bin-linux file will ensure that we’re downloading the correct version. So now that we have the bin-linux file, we set that to a variable x. With the variable set, we then download $x, which is the Linux zip file. The zip file now exists in our udpateMinecraft directory so the Bash script continues.

unzip -o *.zip
rm *.zip

The unzip command with -o will overwrite the outdated files without being prompted to confirm if we want to overwrite files. This forces the update, then removes the zip file to prevent clutter.

cp perms.txt permissions.json

This one-liner updates the updated (now overwritten) permissions file with our server permissions. This was a headache before because Minecraft isn’t clear on where operator permissions are stored. Now that our users have admin rights, the Bash script continues.

sed -i -e 's/allow-cheats=false/allow-cheats=true/g' updatedMinecraft/server.properties

The sed command searches the server.properties file for the string “allow-cheats=false” and updates that with “allow-cheats=true”. Yes, cheating is bad but I need that to be able to teleport to my kid who is always getting lost in the Minecraft world.

 chmod +x bedrock_server
 done 

Finally, chmod +x makes the bedrock_server file executable. The Bash script is now complete. We can now run the bedrock_server executable with ./bedrock_server and join the game with our correct settings. Thanks for reading and feel free to reach out if you have any recommendations to tidy up my Bash script.