All-in-one Minecraft server manager (bash)

By tmad40blue on Jan 19, 2016

Quick and dirty. Provides the basics for starting, stopping, viewing/using console, and updating to latest Spigot.

TODO: Possibly run as a system service.

#! /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: mcserver

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: minecraft_server.jar

USERNAME="" # The name of the user (hopefully you) who is running the server - 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 new-session -d -s mcserver -n 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 "\n \n \n"
  while true; do
    read -e -i "y" -p "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
    echo -e "$(tput setaf 2)Update complete! $(tput sgr 0)Cleaning up..."
    cd $DIRECTORY
    cp _buildtools/spigot-*.jar ${SERVDIR}/spigot.jar
    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.0 $(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

Comments

Sign in to comment.
Hawkee   -  Jan 19, 2016

Nice, so is Spigot the new Bukkit? It seems to be the emerging winner.

tmad40blue  -  Jan 19, 2016

Yep. When Microsoft acquired Mojang in August 2014, it was revealed that they had owned Bukkit for over 2 years. Obviously all the people who had been contributing code to the Bukkit project weren't so happy about their contributions being put into the game and sold without their knowledge, so they put forth a DMCA on all the Bukkit code and attempted to take down the project. In response, Mojang privatized and subsequently deleted all of the Bukkit repositories, and made it illegal to redistribute the Minecraft server software in any way (Bukkit included).

The Spigot team then swooped in and took over. Nowadays you're required to build Spigot on your own local machine through BuildTools, which basically downloads the vanilla Minecraft server JAR from Mojang, decompiles/patches/does magic to it, and builds Spigot right there locally. They also maintain Bukkit/CraftBukkit as a convenience, but Spigot is definitely the king (since it has tons of bugfixes, improvements, and performance tweaks) and is 99.99% backwards-compatible with all Bukkit plugins.

The Spigot team has been responsible for the last several official Minecraft updates because they find and fix security flaws months faster than Mojang does.

Hawkee  -  Jan 20, 2016

Good to know! I should probably update my Bukkit server to Spigot. Now just to find the time..

SReject  -  Feb 07, 2016

There's alot of miss-information by tmad40blue, but the gist of it is correct. To clearify what actually happened:

The original maintainers of bukkit, after spending 3+yrs on the project, decided to end the project for a number of valid reasons(EULA, personal strife, etc), so they made a post stating that bukkit would not be updated to 1.8. One of Mojang's Employees then stated in a tweet that "bukkit belongs to Mojang", and "I'll personally be updating bukkit to 1.8". Which is what pissed off the very involved developer Wolvereness (amoung many other contributors to bukkit/craftbukkit). He made a DMCA against bukkit/craftbukkit, spigot and others that based their code on the craftbukkit project.

This all played out over the course of a month, during which the Minecraft community roasted Notch(the largest shareholder of Mojang) for something he hasn't been involved in for 2yrs. He got tired of the BS and sold his share to Microsoft.

Spigot has risen as the craftbukkit replacement due to being the only drop-in replacement for bukkit when the project faltered. With that said, its far from being as comparable to vanilla as craftbukkit was. Before bukkit fell, spigot aimed at server-side performance; to do such they took leisure with mechanics such as mob AI, and redstone ticks. Furthermore, the spigot project's lead maintainer is not, errr, open to fixing failings of the Bukkit API nor is he willing to give up the mentality of "High performance MC server!" So if you expect for vanilla gameplay it may be better to look elsewhere which is why core members of the spigot team have been working on a new project called Paper Spigot

Sources:
EvilSeph discontinues bukkit project
Playout between EvilSeph and Mojang
Archive of Wolvereness DMCA - Offending pages have been deleted so had to resort to an archive

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.