Skip to content

Instantly share code, notes, and snippets.

@victornpb
Last active April 20, 2024 13:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victornpb/e283c4f30c93ba7f8e087d1d978e44f0 to your computer and use it in GitHub Desktop.
Save victornpb/e283c4f30c93ba7f8e087d1d978e44f0 to your computer and use it in GitHub Desktop.
Bash script to start Minecraft server and keep it running if it crashes
#!/bin/bash
# Config
WINDOW_TITLE="Minecraft Server"
ONLINE_AUTH=true
RAM_INITIAL=512M
RAM_MAX=4G
RESTART_DELAY=10
################################################################################
cd "$(dirname "$0")"
while true
do
# Find jar file
jarfile=$(ls | grep -i -e "\.jar$" | head -1)
if [ -e $jarfile ]; then
echo " STARTING SERVER..."
echo "==============================================================================="
echo -n -e "\033]0;[RUNNING] $WINDOW_TITLE\007"
java -Xms$RAM_INITIAL -Xmx$RAM_MAX -jar "$jarfile" -o $ONLINE_AUTH nogui > /dev/null
echo -n -e "\033]0;[STOPPED!] $WINDOW_TITLE\007"
echo "###############################################################################"
echo " [$(date)] SERVER STOPPED!"
else
echo "Could not find the server jar file."
fi
echo " Rebooting in $RESTART_DELAY seconds... (Press CTRL-C to kill process)"
sleep $RESTART_DELAY
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment