Source : http://vaslor.net/game/mc/mcm

SERVICE에 jar 이름 적어주고, MCPATH에 경로 넣어준다.

Username에는 자기 계정명.

java랑 screen 필요

32비트나 openjre로 구동할 경우 INVOCATION에서 -d64 옵션을 삭제한다.


#!/bin/bash
# /etc/init.d/minecraft
# version 0.3.2 2011-01-27 (YYYY-MM-DD)
 
  ### BEGIN INIT INFO
  # Provides:   minecraft
  # Required-Start: $local_fs $remote_fs
  # 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:    Minecraft server
  # Description:    Starts the minecraft server
  ### END INIT INFO
 
#Settings
SERVICE='craftbukkit.jar'
USERNAME="mine"
MCPATH='/home/mine/mcone'
INVOCATION='java -XX:UseSSE=4 -server -d64 -XX:+CMSIncrementalPacing -XX:+UseConcMarkSweepGC -XX:SurvivorRatio=16 -XX:+UseParNewGC -XX:ParallelGCThreads=2 -Xmx4G -Xms2G -jar craftbukkit.jar nogui'
BACKUPPATH='/mnt/data/backup'
 
ME=`whoami`
as_user() {
  if [ "$ME" == "$USERNAME" ] ; then
    bash -c "$1"
  else
    su - $USERNAME -c "$1"
  fi
}
 
mc_start() {
  if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  then
    echo "Tried to start but $SERVICE was already running!"
  else
    echo "$SERVICE was not running... starting."
    cd $MCPATH
    as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
    sleep 7
    if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
    then
      echo "$SERVICE is now running."
    else
      echo "Could not start $SERVICE."
    fi
  fi
}
 
mc_saveoff() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
	then
		echo "$SERVICE is running... suspending saves"
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
                sync
		sleep 10
	else
                echo "$SERVICE was not running. Not suspending saves."
	fi
}
 
 
mc_saveall() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
                echo "$SERVICE is running... Saving world..."
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"who\"\015'"
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
        else
                echo "$SERVICE was not running. Not resuming saves."
        fi
}
 
mc_saveon() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
	then
		echo "$SERVICE is running... re-enabling saves"
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
	else
                echo "$SERVICE was not running. Not resuming saves."
	fi
}
 
mc_stop() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
                echo "$SERVICE is running... stopping."
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"say 60초 후 서버가 자동으로 종료됩니다. 로그아웃해주세요.\"\015'"
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
                sleep 60
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
 
        else
                echo "$SERVICE was not running."
        fi
        echo "Waiting shutdown process..."
         sleep 10
 
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
                echo "$SERVICE could not be shut down... still running."
        else
                echo "$SERVICE is shut down."
        fi
}
 
 
mc_update() {
  if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  then
    echo "$SERVICE is running! Will not start update."
  else
    MC_SERVER_URL=http://minecraft.net/`wget -q -O - http://www.minecraft.net/download.jsp | grep minecraft_server.jar\</a\> | cut -d \" -f 2`
    as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update $MC_SERVER_URL"
    if [ -f $MCPATH/minecraft_server.jar.update ]
    then
      as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/minecraft_server.jar"
      echo "Minecraft successfully updated."
    else
      echo "Minecraft update could not be downloaded."
    fi
  fi
}
 
mc_backup() {
  if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  then
#    echo "$SERVICE is running! Will not start backup."
#  else
    echo "Backing up minecraft data"
    if [ -d $BACKUPPATH/world_`date "+%m.%d.%Y"` ]
    then
      for i in 1 2 3 4 5 6
      do
        if [ -d $BACKUPPATH/world_`date "+%m.%d.%Y"`-$i ]
        then
          continue
        else
          as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%m.%d.%Y"`-$i"
          break
        fi
      done
    else
      as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%m.%d.%Y"`"
    fi
  fi
  echo "Backup complete"
}
 
mc_fullbackup() {
  if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  then
# 실행 중에만 백업
    echo "Full-Backing up minecraft"
          as_user "cd $MCPATH && tar cvfz $BACKUPPATH/mc`date "+%Y-%m-%d_%H%M"`.tar.gz . > /dev/null"
 
   echo "Full-Backup complete"
    fi
}
 
 
#Start-Stop here
date
case "$1" in
  start)
    mc_start
    ;;
  stop)
    mc_stop
    ;;
  restart)
    mc_stop
    sleep 60
    mc_start
    ;;
  update)
    mc_stop
    mc_backup
    mc_update
    mc_start
    ;;
 saveall)
   mc_saveall
   ;;
 
 fullbackup)
    mc_saveoff
    mc_fullbackup
    mc_saveon
    ;;
 
  backup)
    mc_saveoff
    mc_backup
    mc_saveon
    ;;
  status)
    if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
    then
      echo "$(tput setaf 3)$SERVICE$(tput setaf 9) is  $(tput setaf 2)running.$(tput setaf 9)"
      tail -7 "$MCPATH/server.log"
      echo "$(tput setaf 6)"
      ps  ux | grep -v " 0.0" |head -c150
      echo "$(tput setaf 3)"
      free -m
      echo "$(tput setaf 9)"
    else
      echo "$SERVICE is not running."
    fi
    ;;
 
  *)
  echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|saveall|status|restart|fullbackup}"
  exit 1
  ;;
esac
 
exit 0




'Application > Java' 카테고리의 다른 글

마인크래프트 서버, 서비스로 구동  (0) 2014.03.02
Java에 대한 보이지 않는 위협  (0) 2008.02.21

+ Recent posts