Skip to content

Instantly share code, notes, and snippets.

@Sotalbireo
Last active October 13, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sotalbireo/6364b210c4d6f1499d735c1e931a8e6c to your computer and use it in GitHub Desktop.
Save Sotalbireo/6364b210c4d6f1499d735c1e931a8e6c to your computer and use it in GitHub Desktop.
VPSでMinecraftを立ち上げるためのなんやかんや。どこから拾ったか忘れた。
#!/bin/bash
# /etc/init.d/minecraft
# Original: http://minecraft.gamepedia.com/Tutorials/Server_startup_script
# version 0.4.2 2016-02-09 (YYYY-MM-DD)
# Arranged: https://gist.github.com/Sotalbireo/6364b210c4d6f1499d735c1e931a8e6c
# version 0.0.5 2016-10-14 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs screen-cleanup
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manager of the Minecraft server
# Description: Manage the minecraft server; start, stop, backup, update, etc.
### END INIT INFO
#Settings
readonly SERVICE='minecraft_server.jar'
readonly SCREENNAME='minecraft_server'
readonly OPTIONS='nogui'
readonly USERNAME='minecraft'
readonly WORLD='world'
readonly MC_DIR='/usr/local/minecraft'
readonly BACKUP_DIR="${MC_DIR}/.backups"
readonly HEAP_MAX=1536 # MB
readonly PERM_MAX=256 # MB
readonly HISTORY=512 # Lines
readonly CPU_COUNT=2 # Core(s)
readonly INVOCATION="java -Xmx${HEAP_MAX}M -Xms${HEAP_MAX}M -XX:PermSize=${PERM_MAX}m -XX:MaxPermSize=${PERM_MAX}m -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE $OPTIONS"
readonly 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 $MC_DIR
as_user "cd $MC_DIR && screen -h $HISTORY -U -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!"
exit 1
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 $MC_DIR && wget -q -O $MC_DIR/versions --no-check-certificate https://launchermeta.mojang.com/mc/game/version_manifest.json"
if [ "$1" == "snapshot" ]; then
_JSON_VERSION=`cd $MC_DIR && 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
_JSON_VERSION=`cd $MC_DIR && 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 $MC_DIR && wget -q -O $MC_DIR/versions --no-check-certificate ${_JSON_VERSION}"
_MC_SERVER_URL=`cd $MC_DIR && cat versions | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["downloads"]["server"]["url"])'`
as_user "rm $MC_DIR/versions"
as_user "cd $MC_DIR && wget -q -O $MC_DIR/minecraft_server.jar.update --no-check-certificate ${_MC_SERVER_URL}"
if [ -f $MC_DIR/minecraft_server.jar.update ]; then
if `diff $MC_DIR/$SERVICE $MC_DIR/minecraft_server.jar.update >/dev/null` ; then
echo "You are already running the latest version of $SERVICE."
exit 2
else
as_user "mv $MC_DIR/minecraft_server.jar.update $MC_DIR/$SERVICE"
echo "Minecraft successfully updated."
exit 0
fi
else
echo "Minecraft update could not be downloaded."
exit 1
fi
fi
}
mc_backup() {
mc_saveoff
_NOW=`date "+%Y-%m-%d_%Hh%M"`
_BACKUP_FILE="$BACKUP_DIR/${WORLD}_${_NOW}.tar"
echo "Backing up minecraft world..."
as_user "tar -C \"$MC_DIR\" -cf \"${_BACKUP_FILE}\" $WORLD"
echo "Backing up $SERVICE..."
as_user "tar -C \"$MC_DIR\" -rf \"${_BACKUP_FILE}\" $SERVICE"
mc_saveon
echo "Compressing backup..."
as_user "gzip -f \"${_BACKUP_FILE}\""
if [ -f "${_BACKUP_FILE}" ]; then
echo "Done."
else
echo "Error: Backup file is NOT exists."
fi
}
mc_command() {
_command="$1"
if pgrep -u $USERNAME -f $SERVICE >/dev/null; then
pre_log_len=`wc -l "$MC_DIR/logs/latest.log" | awk '{print $1}'`
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"${_command}\"\015'"
sleep .2 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $((`wc -l "$MC_DIR/logs/latest.log" | awk '{print $1}'`-$pre_log_len)) "$MC_DIR/logs/latest.log"
else
exit 2
fi
}
mc_init(){
echo "Starting to $SERVICE's server setting up..."
echo "[1/] Check directory: $MC_DIR"
if [[ -f "${MC_DIR}" ]]; then
echo "...Done."
else
printf "..."
as_user "mkdir -rfv ${MC_DIR}"
if [[ -f "${MC_DIR}" ]]; then
echo "...Done."
else
echo "Error! $?"
echo "Directory(${MC_DIR}) does NOT exists."
exit 2
fi
fi
}
mc_usage() {
cat <<EOS
Manager of the Minecraft server
Usage: $0 command [options]
List of Commands:
start Start the Server.
stop Stop the Server.
update Update the Server.
backup Backups to tar file.
status Check whether running the server.
restart Restart the Server.
exec Doing command at inside the server
update options:
snapshot Update with latest SNAPSHOT release.
latest Update with latest MAJOR VERSION release.
exec command's example:
- exec "list"
showing joined users list.
- exec "say Lorem Ipsum"
tells "Lorem Ipsum" at all users.
EOS
exit 1
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
update)
mc_stop
mc_backup
mc_update $2
mc_start
;;
backup)
mc_backup
;;
status)
if pgrep -u $USERNAME -f $SERVICE >/dev/null; then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
exec)
if [ $# -gt 1 ]; then
shift
mc_command "$*"
else
echo "Must specify server command (try 'help'?)"
exit 1
fi
;;
init)
mc_init
;;
*)
mc_usage
;;
esac
exit $?
@Sotalbireo
Copy link
Author

ひとまず今動かしてる奴をそのまま。これから修正していきます。

@Sotalbireo
Copy link
Author

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