ME=`whoami` as_user() { if [ "$ME" = "$USERNAME" ] ; then bash -c "$1" else su - "$USERNAME" -c "$1" fi }
mc_start() { if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "$SERVICE is already running!" else echo "Starting $SERVICE..." cd $MCPATH as_user "cd $MCPATH && screen -h $HISTORY -dmS ${SCREENNAME} $INVOCATION" sleep 7 if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "$SERVICE is now running." else echo "Error! Could not start $SERVICE!" fi fi }
mc_saveoff() { if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "$SERVICE is running... suspending saves" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-off\"\015'" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'" sync sleep 10 else echo "$SERVICE is not running. Not suspending saves." fi }
mc_saveon() { if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "$SERVICE is running... re-enabling saves" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-on\"\015'" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'" else echo "$SERVICE is not running. Not resuming saves." fi }
mc_stop() { if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "Stopping $SERVICE" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'" as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'" sleep 10 as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"stop\"\015'" sleep 7 else echo "$SERVICE was not running." fi if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "Error! $SERVICE could not be stopped." else echo "$SERVICE is stopped." fi }
mc_update() { if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then echo "$SERVICE is running! Will not start update." else as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate https://launchermeta.mojang.com/mc/game/version_manifest.json" if [ "$1" == "snapshot" ] ; then JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['snapshot']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"` else JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['release']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"` fi as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate $JSONVERSION" MC_SERVER_URL=`cd $MCPATH && cat versions | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["downloads"]["server"]["url"])'` as_user "rm $MCPATH/versions" as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update --no-check-certificate $MC_SERVER_URL" if [ -f $MCPATH/minecraft_server.jar.update ] ; then if `diff $MCPATH/$SERVICE $MCPATH/minecraft_server.jar.update >/dev/null` ; then echo "You are already running the latest version of $SERVICE." else as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/$SERVICE" echo "Minecraft successfully updated." fi else echo "Minecraft update could not be downloaded." fi fi }