Skip to content

Instantly share code, notes, and snippets.

@ariela

ariela/minecraft.sh

Created Aug 2, 2011
Embed
What would you like to do?
minecraftのサーバ起動スクリプト(centosでのみテスト)
#!/bin/sh
#
# minecraft Startup script for the minecraft Server
#
# chkconfig: - 90 25
# description: Minecraft is Sandbox game server.
# processname: minecraft
# パス設定
MC_PATH_HOME="/opt/minecraft" # minecraftサーバの基本位置
MC_PATH_SERVER="${MC_PATH_HOME}/server" # サーバプログラム配置位置
MC_PATH_BACKUP="${MC_PATH_HOME}/backup" # バックアップ配置位置
MC_PATH_DATA="${MC_PATH_HOME}/data" # サーバデータ配置位置
MC_PATH_BIN="${MC_PATH_SERVER}/bin" # サーバプログラム本体設置位置
# マシン設定
MC_USE_CPU=2 # GCに使用するCPU数
MC_USE_MEM="2048M" # 割り当てるメモリ数
# Minecraft設定
MC_USER="minecraft" # サーバプログラムの実行ユーザ
MC_SERVICE="minecraft_server.jar" # サーバプログラム名
MC_OPTION="nogui" # サーバプログラムオプション
MC_STOP_INTERVAL=60 # サーバ停止時の待ち時間
MC_SCREENNAME="minecraft-server" # screenの名称
# minecraftを実行するコマンド
MC_INVOCATION="java -Xmx${MC_USE_MEM} -Xms${MC_USE_MEM} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=${MC_USE_CPU} -XX:+AggressiveOpts -jar ${MC_PATH_SERVER}/${MC_SERVICE} ${MC_OPTION}"
# バックアップ設定
MC_BACKUP_FILE=`date '+%Y%m%d-%H'`
MC_BACKUP_PREF_WORLD="world-"
MC_BACKUP_PREF_NETHER="nether-"
MC_BACKUP_PREF_SERVER="server-"
MC_BACKUP_NAME_WORLD="world"
MC_BACKUP_NAME_NETHER="world_nether"
# 更新設定
#CraftBukkitの場合
MC_DOWNLOAD_FILE="craftbukkit-0.0.1-SNAPSHOT.jar"
MC_DOWNLOAD_URI="http://ci.bukkit.org/job/dev-CraftBukkit/lastSuccessfulBuild/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar"
# 公式サーバの場合
#MC_DOWNLOAD_FILE="minecraft_server.jar"
#MC_DOWNLOAD_URI=http://www.minecraft.net/download/minecraft_server.jar?v=`date | sed "s/[^a-zA-Z0-9]/_/g"`
# コマンド送信
MC_SEND="screen -p 0 -S ${MC_SCREENNAME} -X eval"
# 表示設定
RESET=$'\e[0m'
BOLD=$'\e[1m'
RED=$'\e[1;31m'
GREEN=$'\e[1;32m'
# ユーザ別実行 #####################################################################################
ME=`whoami`
as_user() {
if [ ${ME} == ${MC_USER} ] ; then
bash -c "$1"
else
su - ${MC_USER} -c "$1"
fi
}
# 起動処理 #########################################################################################
mc_start() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft: [${GREEN}起動中${RESET}]"
else
echo "minecraft: [${BOLD}起動処理中...${RESET}]"
cd ${MC_PATH_DATA}
as_user "cd ${MC_PATH_DATA} && screen -AmdS ${MC_SCREENNAME} ${MC_INVOCATION}"
sleep 7
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft を起動中: [${GREEN}OK${RESET}]"
else
echo "minecraft を起動中: [${RED}NG${RESET}]"
fi
fi
}
# 停止処理 #########################################################################################
mc_stop() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft: [${BOLD}停止処理中...${RESET}]"
as_user "${MC_SEND} 'stuff \"say SERVER SHUTTING DOWN IN ${MC_STOP_INTERVAL} SECONDS. Saving map...\"\015'"
i=${MC_STOP_INTERVAL}
while [ ${i} -ne 0 ]
do
if test `expr ${i} % 30` -eq 0 -o ${i} -le 10
then
as_user "${MC_SEND} 'stuff \"say SERVER WILL STOP IN ${i} SECONDS.\"\015'"
fi
i=`expr ${i} - 1`
sleep 1
done
as_user "${MC_SEND} 'stuff \"save-all\"\015'"
as_user "${MC_SEND} 'stuff \"stop\"\015'"
sleep 7
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft を停止中: [${RED}NG${RESET}]"
else
echo "minecraft を停止中: [${GREEN}OK${RESET}]"
fi
else
echo "minecraft: [${RED}停止中${RESET}]"
fi
}
# ステータスチェック処理 ###########################################################################
mc_status() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft: [${GREEN}起動中${RESET}]"
else
echo "minecraft: [${RED}停止中${RESET}]"
fi
}
# バックアップ処理 #################################################################################
mc_backup_world() {
ARCFILE=${MC_PATH_BACKUP}/${MC_BACKUP_PREF_WORLD}${MC_BACKUP_FILE}.tgz
TRGFILE=${MC_BACKUP_NAME_WORLD}
echo "minecraft: [${BOLD}${MC_BACKUP_NAME_WORLD} バックアップ中...${RESET}]"
as_user "cd ${MC_PATH_DATA} && tar -czf ${ARCFILE} ${TRGFILE}"
ARCFILE=${MC_PATH_BACKUP}/${MC_BACKUP_PREF_NETHER}${MC_BACKUP_FILE}.tgz
TRGFILE=${MC_BACKUP_NAME_NETHER}
echo "minecraft: [${BOLD}${MC_BACKUP_NAME_NETHER} バックアップ中...${RESET}]"
as_user "cd ${MC_PATH_DATA} && tar -czf ${ARCFILE} ${TRGFILE}"
echo "minecraft: [${GREEN}バックアップ完了${RESET}]"
}
mc_backup_server() {
ARCFILE=${MC_PATH_BACKUP}/${MC_BACKUP_PREF_SERVER}${MC_BACKUP_FILE}.tgz
TRGFILE=${MC_SERVICE}
echo "minecraft: [${BOLD}${MC_SERVICE} バックアップ中...${RESET}]"
as_user "cd ${MC_PATH_SERVER} && tar -czf ${ARCFILE} ${TRGFILE}"
echo "minecraft: [${GREEN}バックアップ完了${RESET}]"
}
# サーバプログラムアップデート #####################################################################
mc_update() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep ${MC_SERVICE} > /dev/null
then
echo "minecraft: [${RED}サーバが起動中なのでアップデートできません。${RESET}]"
else
echo "minecraft: [${BOLD}サーバアップデート中...${RESET}]"
as_user "cd ${MC_PATH_BIN} && wget -q -O ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE}.update ${MC_DOWNLOAD_URI}"
if [ -f ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE}.update ]
then
if `diff ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE} $MC_PATH_BIN/${MC_DOWNLOAD_FILE}.update >/dev/null`
then
echo "minecraft: [${GREEN}${MC_DOWNLOAD_FILE}は最新版です${RESET}]"
as_user "rm -f ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE}.update"
else
as_user "mv ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE}.update ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE}"
as_user "ln -sf ${MC_PATH_BIN}/${MC_DOWNLOAD_FILE} ${MC_PATH_SERVER}/${MC_SERVICE}"
echo "minecraft をアップデート: [${GREEN}OK${RESET}]"
fi
else
echo "minecraft をアップデート: [${RED}NG${RESET}"
fi
fi
}
# 処理分岐 #########################################################################################
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
as_user "${MC_SEND} 'stuff \"say SERVER WILL RESTART! PLEASE LOGOUT!\"\015'"
mc_stop
mc_start
;;
backup)
mc_backup_world
;;
server-backup)
mc_backup_server
;;
status)
mc_status
;;
update)
mc_backup_server
mc_update
;;
*)
echo "使い方: minecraft {start|stop|restart|backup|server-backup|status|update}"
esac
exit 0
@goodall-seiya

This comment has been minimized.

Copy link

@goodall-seiya goodall-seiya commented Oct 25, 2014

すばらしい!ぜひ使わせていただきます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment