Skip to content

Instantly share code, notes, and snippets.

@tmad40blue
Last active April 2, 2016 13:12
Show Gist options
  • Save tmad40blue/b106bba7e0bff6011af3 to your computer and use it in GitHub Desktop.
Save tmad40blue/b106bba7e0bff6011af3 to your computer and use it in GitHub Desktop.
All-in-one Minecraft server management script.
#! /bin/bash
set -o errexit
# Minecraft Server Management Script
# By tmad40blue - http://tmad40.blue/
# !!! EDIT THE VARIABLES BELOW TO MATCH YOUR SERVER'S INFO !!!
# !!! DO NOT EDIT ANY OTHER PART OF THIS SCRIPT !!!
SERVDIR="" # The complete directory path where your JAR file is - example: /home/minecraft/servers/
SERVNAME="" # What you want your server's process to be called - lower-case, no spaces please - example: mycoolserver
MAXRAM="" # How much RAM (in GB) you want to allocate to this Minecraft server - example: 4
JARFILE="" # The full name of your server JAR - example: spigot.jar
USERNAME="" # The name of the user who is running the server - NOT your Minecraft username - example: tmad40blue
# !!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!! #
# !!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!! #
# !!! DO NOT EDIT BELOW THIS LINE !!! #
# !!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!! #
# !!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!! #
STARTCMD="java\ -Dname=${SERVNAME}\ -d64\ -server\ -Xms512M\ -Xmx${MAXRAM}G\ -XX:+UseG1GC\ -XX:MaxGCPauseMillis=50\ -jar\ ${JARFILE}"
ME=`whoami`
# Functions
as_user() {
if [ "$ME" = "$USERNAME" ] ; then
bash -c "$1"
else
su - "$USERNAME" -c "$1"
fi
}
is_running() {
# echo "DEBUG: U: ${USERNAME} S: {$SERVNAME}"
return `pgrep -u ${USERNAME} -f "$SERVNAME" > /dev/null`
}
server_start() {
if is_running ; then
echo -e "$(tput setaf 1)$(tput bold)ERROR: $(tput sgr 0)Server is already running!"
else
echo -e "$(tput dim)Starting $(tput setaf 3)$SERVNAME$(tput sgr 0)..."
as_user "tmux start-server"
tmux has-session -t mcserver &> /dev/null
if [ $? != 0 ] ; then
as_user "tmux new -A -s mcserver -d"
fi
as_user "tmux send-keys -t mcserver {$STARTCMD}"
as_user "tmux send-keys -t mcserver Enter"
sleep 3
if is_running ; then
echo -e "$(tput setaf 2)Server is now running.$(tput sgr 0)"
else
echo -e "$(tput setaf 1)$(tput bold)ERROR: $(tput sgr 0)Server did not start!"
fi
fi
}
server_stop() {
if is_running ; then
echo -e "$(tput dim)Saving $(tput setaf 3)$SERVNAME$(tput sgr 0)..."
as_user "tmux send-keys -t mcserver "save-all" Enter"
sleep 5
echo -e "$(tput dim)Stopping $(tput setaf 3)$SERVNAME$(tput sgr 0)..."
as_user "tmux send-keys -t mcserver "stop" Enter"
counter=0
while is_running ; do
sleep 1
let counter=counter+1
if [ "${counter}" -gt "1" ]; then
echo -e "$(tput dim)${counter}$(tput sgr 0)"
fi
done
# as_user "tmux kill-session -t mcserver" &
echo -e "$(tput setaf 2)Server stopped.$(tput sgr 0)"
else
echo -e "$(tput setaf 1)$(tput bold)ERROR: $(tput sgr 0)Server is not running!"
fi
}
server_console() {
echo -e "$(tput setaf 3)Opening server console..."
echo -e "$(tput setaf 1)$(tput bold)$(tput blink)!!! PRESS $(tput setaf 5)Ctrl-B $(tput setaf 1)THEN $(tput setaf 5)D $(tput setaf 1)TO EXIT CONSOLE !!! $(tput sgr 0)"
echo -e "$(tput setaf 1)This means that you press $(tput setaf 5)Ctrl-B$(tput setaf 1) at the same time, then LET GO OF THE KEYBOARD, and finally press $(tput setaf 5)D$(tput setaf 1), in that order."
echo -e "\n \n \n"
while true; do
read -e -i "y" -p "$(tput sgr 0)Continue? $(tput dim)[$(tput setaf 2)y$(tput sgr 0)$(tput dim)/$(tput setaf 1)n$(tput sgr 0)$(tput dim)] $(tput sgr 0)" yn
case $yn in
[Yy]*)
break
;;
[Nn]*)
echo -e "$(tput dim)Exiting. $(tput sgr 0)"; return
;;
*)
echo "Please answer 'y' or 'n'."
;;
esac
done
as_user "tmux a -t mcserver"
}
server_update() {
if is_running ; then
echo -e "$(tput setaf 1)$(tput bold)ERROR: $(tput sgr 0)Cannot update server while it is still running!"
else
echo -e "$(tput setaf 3)Updating to latest Spigot...$(tput sgr 0)"
echo -e "$(tput setaf 1)$(tput bold)$(tput blink)!!! THIS WILL TAKE A LONG TIME !!!$(tput sgr 0)"
sleep 3
cd $SERVDIR
rm -rf .m2
rm -rf _buildtools
mkdir "_buildtools"
cd "_buildtools"
wget -O "BuildTools.jar" "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar"
# git config --global --unset core.autocrlf
sleep 2
java -d64 -Xms512M -Xmx${MAXRAM}G -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -jar BuildTools.jar --rev latest
echo -e "$(tput setaf 2)Update complete! $(tput sgr 0)Cleaning up..."
cp ./spigot-*.jar ${SERVDIR}/${JARFILE}
rm -rf .m2
rm -rf _buildtools
tput clear
echo "$(tput setaf 2)All done! $(tput sgr 0)The latest Spigot should now be in your server directory."
fi
}
echo -e "\n \n \n"
echo -e "$(tput bold)$(tput smul)Minecraft Server Management Script$(tput rmul) $(tput sgr 0)"
echo -e "$(tput bold)Written by $(tput setaf 6)tmad40blue $(tput sgr 0) -- $(tput setaf 4)http://tmad40.blue/ $(tput sgr 0)"
echo -e "$(tput dim)Version 1.2 $(tput sgr 0)"
echo -e "\n \n \n"
sleep 1
# Command processing
case "$1" in
start)
server_start
;;
stop)
server_stop
;;
update)
server_update
;;
console)
server_console
;;
restart)
server_stop
server_start
;;
running)
if is_running ; then
echo -e "$(tput setaf 2)Server is running. $(tput sgr 0)"
else
echo -e "$(tput setaf 1)Server is not running. $(tput sgr 0)"
fi
;;
*)
echo -e "Usage: $(tput setaf 2)$0 $(tput sgr 0)$(tput dim){start|stop|restart|console|running|update}$(tput sgr 0)"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment