Created
June 12, 2022 22:12
-
-
Save robeden/833ab2726f9d63e32eae4c7c19b27642 to your computer and use it in GitHub Desktop.
Auto-update Minecraft server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
# Instructions: place in /srv/minecraft-server | |
# cron to check every 4 hours: | |
# 0 */4 * * * /src/minecraft-server/mc_update.sh | |
cd $(dirname $0) | |
MANIFEST_URL=https://launchermeta.mojang.com/mc/game/version_manifest.json | |
MANIFEST_DATA=`curl --silent $MANIFEST_URL` | |
VERSION=`echo $MANIFEST_DATA | jq -r .latest.release` | |
VERSION_URL=`echo $MANIFEST_DATA | jq -r '.latest.release as $latest | .versions | .[] | select(.id == $latest) | .url'` | |
echo "Latest version: $VERSION" | |
VERSION_DATA=`curl --silent $VERSION_URL | jq .downloads.server` | |
SHA=`echo $VERSION_DATA | jq -r .sha1` | |
URL=`echo $VERSION_DATA | jq -r .url` | |
FILENAME="minecraft_server_$VERSION.jar" | |
if [ -f $FILENAME ]; then | |
CURRENT_SHA=`sha1sum $FILENAME | awk '{ print $1 }'` | |
if [ "$SHA" = "$CURRENT_SHA" ]; then | |
echo "Up-to-date" | |
exit 0 | |
else | |
echo "Checksum mismatch, redownloading..." | |
fi | |
fi | |
echo "Downloading from $URL (checksum $SHA)..." | |
curl --silent $URL > $FILENAME | |
CURRENT_SHA=`sha1sum $FILENAME | awk '{ print $1 }'` | |
if [ "$SHA" != "$CURRENT_SHA" ]; then | |
echo "Checksum mismatch: $SHA != $CURRENT_SHA" | |
exit 1 | |
fi | |
echo "Download complete" | |
ln -sf $FILENAME "minecraft_server.jar" | |
echo "(Re)link complete" | |
service minecraft-server restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment