Skip to content

Instantly share code, notes, and snippets.

@maddiesch
Created July 10, 2013 03:43
Show Gist options
  • Save maddiesch/5963319 to your computer and use it in GitHub Desktop.
Save maddiesch/5963319 to your computer and use it in GitHub Desktop.
A simple bash script for starting a minecraft server. Allows passing a couple command line arguments for memory and game type.
#!/bin/bash
###
## A simple bash script for starting a minecraft server
##
## Options:
## -g : Sets the game mode. 0|1 (default 0)
## -m : Sets the amount of memory to start with. (default 756)
## -t : Sets the memory type. (default M)
##
###
# The directory where the minecraft server data is. If this script is in the same directory just set it to `.`
MINECRAFT_DIRECTORY=mc_data
# The name of the .jar file for the server
MINECRAFT_SERVER_FILE=minecraft_server.jar
# Read options
while getopts g:m:t: option
do
case "${option}"
in
g) GAME=${OPTARG};;
m) MEMORY=${OPTARG};;
t) MEMORYTYPE=${OPTARG};;
esac
done
# Check for a game type
if [[ -z "$GAME" ]]
then
GAME=0
fi
# Check for the amount of memory
if [[ -z "$MEMORY" ]]
then
MEMORY=756
fi
# Check for memory type
if [[ -z "$MEMORYTYPE" ]]
then
MEMORYTYPE="M"
fi
cd $MINECRAFT_DIRECTORY
echo "Setting game mode..."
# Set Game Mode
PROP=""
while read line
do
if [[ $line == gamemode=* ]]
then
MODE="gamemode=$GAME"
PROP="$PROP$MODE\n"
else
PROP="$PROP$line\n"
fi
done < server.properties
echo -e $PROP > server.properties
# Start the server
echo "Starting server..."
java -Xmx$MEMORY$MEMORYTYPE -Xms$MEMORY$MEMORYTYPE -jar $MINECRAFT_SERVER_FILE nogui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment