Skip to content

Instantly share code, notes, and snippets.

@simbo
Created March 7, 2013 14:46
Show Gist options
  • Save simbo/5108512 to your computer and use it in GitHub Desktop.
Save simbo/5108512 to your computer and use it in GitHub Desktop.
shell script for Minecraft server management
#!/bin/bash
#
# Minecraft Server Script
#
# Author: Simon Lepel
# Version: 0.1 alpha
# License: GNU GPL
# Requirements: screen
# Configuration
################################################################################
MC_PATH=/home/mineblock/bukkit
MC_JAR=craftbukkit-0.0.1-SNAPSHOT.jar
MC_WORLD="mineblock"
ALOC_XMX=2048
ALOC_XMS=2048
BACKUP_PATH=$MC_PATH/backup
SCREEN_NAME=$MC_WORLD
################################################################################
COLOR_TEXT="\033[1;37m"
COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[0;32m"
COLOR_YELLOW="\033[1;33m"
COLOR_NONE="\033[0m"
MSG_START="${COLOR_TEXT}Starting Minecraft server...${COLOR_NONE}"
MSG_STOP="${COLOR_TEXT}Stopping Minecraft server...${COLOR_NONE}"
MSG_ONLINE="${COLOR_TEXT}Minecraft server seems to be ${COLOR_GREEN}ONLINE${COLOR_TEXT}.${COLOR_NONE}"
MSG_OFFLINE="${COLOR_TEXT}Minecraft server seems to be ${COLOR_RED}OFFLINE${COLOR_TEXT}.${COLOR_NONE}"
MSG_HELP="${COLOR_TEXT}Usage: $0 { console | status | start | starthere | stop [now] | restart [now] }${COLOR_NONE}"
MSG_LEAVECONSOLE="${COLOR_TEXT}Press Ctrl-A,D to leave the console${COLOR_NONE}."
MSG_ANNOUNCE="${COLOR_TEXT}Minecraft ingame announcement: ${COLOR_YELLOW}%b${COLOR_NONE}"
ANNOUNCE_RESTART="Server wird neu gestartet."
ANNOUNCE_SHUTDOWN_COUNTDOWN="Stoppe Server in %b Sekunden."
mc_display() {
screen -R $SCREEN_NAME
}
mc_start() {
echo -e $MSG_START
cd $MC_PATH
screen -m -d -S $SCREEN_NAME java -Xincgc -Xmx${ALOC_XMX}M -Xms${ALOC_XMS}M -jar $MC_JAR
}
mc_stop() {
if [ $p2 != "now" ]
then
s=30
while [ $s -gt 0 ]
do
mc_announce `printf "$ANNOUNCE_SHUTDOWN_COUNTDOWN" "$s"`
s=`expr $s - 10`
sleep 10
done
fi
echo -e $MSG_STOP
mc_command stop
}
mc_command() {
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "${*}\r"`"
}
mc_announce() {
mc_command say ${*}
echo -e "`printf "$MSG_ANNOUNCE" "${*}"`"
}
pause_anykey() {
stty -echo
read -n1 -p "(press any key to continue)"
stty echo
echo
}
if [ -e $MC_PATH/server.log.lck ]
then
ONLINE=1
else
ONLINE=0
fi
p1=${1-default}
p2=${2-deafult}
case $p1 in
"console")
if [ $ONLINE -eq 1 ]
then
echo -e $MSG_LEAVECONSOLE
pause_anykey
mc_display
else
echo -e "$MSG_OFFLINE"
fi
;;
"status")
if [ $ONLINE -eq 1 ]
then
echo -e $MSG_ONLINE
else
echo -e $MSG_OFFLINE
fi
;;
"start")
if [ $ONLINE -eq 1 ]
then
echo -e $MSG_ONLINE
else
mc_start
fi
;;
"starthere")
if [ $ONLINE -eq 1 ]
then
echo -e $MSG_ONLINE
else
cd $MC_PATH
java -Xincgc -Xmx${ALOC_XMX}M -Xms${ALOC_XMS}M -jar $MC_JAR
fi
;;
"stop")
if [ $ONLINE -eq 1 ]
then
mc_stop
else
echo -e $MSG_OFFLINE
fi
;;
"restart")
if [ $ONLINE -eq 1 ]
then
mc_announce $ANNOUNCE_RESTART
mc_stop
echo "(waiting 5 seconds)"
sleep 5
mc_start
else
echo -e $MSG_OFFLINE
fi
;;
*)
echo -e $MSG_HELP
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment