Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

minecraft server startup script

By: a guest on May 24th, 2011  |  syntax: Bash  |  size: 5.71 KB  |  views: 281  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3. # version 0.3.2 2011-01-27 (YYYY-MM-DD)
  4.  
  5. ### BEGIN INIT INFO
  6. # Provides:   minecraft
  7. # Required-Start: $local_fs $remote_fs
  8. # Required-Stop:  $local_fs $remote_fs
  9. # Should-Start:   $network
  10. # Should-Stop:    $network
  11. # Default-Start:  2 3 4 5
  12. # Default-Stop:   0 1 6
  13. # Short-Description:    Minecraft server
  14. # Description:    Starts the minecraft server
  15. ### END INIT INFO
  16.  
  17. #Settings
  18. SERVICE='minecraft_server.jar'
  19. USERNAME="minecraft"
  20. MCPATH='/var/minecraft'
  21. CPU_COUNT=1
  22. INVOCATION="java -Xmx1024M -Xms1024M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar minecraft_server.jar nogui"
  23. BACKUPPATH='/data/backups/minecraft/minecraft.backup'
  24.  
  25. ME=`whoami`
  26. as_user() {
  27.   if [ $ME == $USERNAME ] ; then
  28.     bash -c "$1"
  29.   else
  30.     su - $USERNAME -c "$1"
  31.   fi
  32. }
  33.  
  34. mc_start() {
  35.   if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  36.   then
  37.     echo "Tried to start but $SERVICE was already running!"
  38.   else
  39.     echo "$SERVICE was not running... starting."
  40.     cd $MCPATH
  41.     as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
  42.     sleep 7
  43.     if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  44.     then
  45.       echo "$SERVICE is now running."
  46.     else
  47.       echo "Could not start $SERVICE."
  48.     fi
  49.   fi
  50. }
  51.  
  52. mc_saveoff() {
  53.         if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  54.         then
  55.                 echo "$SERVICE is running... suspending saves"
  56.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly... Expect to be disconnected...\"\015'"
  57.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
  58.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
  59.                 sync
  60.                 sleep 10
  61.         else
  62.                 echo "$SERVICE was not running. Not suspending saves."
  63.         fi
  64. }
  65.  
  66. mc_saveon() {
  67.         if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  68.         then
  69.                 echo "$SERVICE is running... re-enabling saves"
  70.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
  71.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
  72.         else
  73.                 echo "$SERVICE was not running. Not resuming saves."
  74.         fi
  75. }
  76.  
  77. mc_stop() {
  78.         if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  79.         then
  80.                 echo "$SERVICE is running... stopping."
  81.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
  82.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
  83.                 sleep 10
  84.                 as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
  85.                 sleep 7
  86.         else
  87.                 echo "$SERVICE was not running."
  88.         fi
  89.         if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  90.         then
  91.                 echo "$SERVICE could not be shut down... still running."
  92.         else
  93.                 echo "$SERVICE is shut down."
  94.         fi
  95. }
  96.  
  97.  
  98. mc_update() {
  99.   if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  100.   then
  101.     echo "$SERVICE is running! Will not start update."
  102.   else
  103.     MC_SERVER_URL=http://www.minecraft.net/download/minecraft_server.jar?v=`date | sed "s/[^a-zA-Z0-9]/_/g"`
  104.     as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update $MC_SERVER_URL"
  105.     if [ -f $MCPATH/minecraft_server.jar.update ]
  106.     then
  107.       if `diff $MCPATH/minecraft_server.jar $MCPATH/minecraft_server.jar.update >/dev/null`
  108.         then
  109.           echo "You are already running the latest version of $SERVICE."
  110.         else
  111.           as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/minecraft_server.jar"
  112.           echo "Minecraft successfully updated."
  113.       fi
  114.     else
  115.       echo "Minecraft update could not be downloaded."
  116.     fi
  117.   fi
  118. }
  119.  
  120. mc_backup() {
  121.    echo "Backing up minecraft world"
  122.    if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"` ]
  123.    then
  124.      for i in 1 2 3 4 5 6
  125.      do
  126.        if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i ]
  127.        then
  128.          continue
  129.        else
  130.          as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i"
  131.          break
  132.        fi
  133.      done
  134.    else
  135.      as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`"
  136.      echo "Backed up world"
  137.    fi
  138.    echo "Backing up the minecraft server executable"
  139.    if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar" ]
  140.    then
  141.      for i in 1 2 3 4 5 6
  142.      do
  143.        if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar" ]
  144.        then
  145.          continue
  146.        else
  147.          as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar\""
  148.          break
  149.        fi
  150.      done
  151.    else
  152.      as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar\""
  153.    fi
  154.    echo "Backup complete"
  155. }
  156.  
  157. mc_backup() {
  158.    if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  159.     then
  160.       echo "$SERVICE is running."
  161.     else
  162.       echo "$SERVICE is not running."
  163.    fi
  164. }
  165.  
  166. mc_say() {
  167.  if ["$2"=""]
  168.   then
  169.    echo "please type a message to say"
  170.   else
  171.    as_user "screen -p 0 -S minecraft -X eval 'stuff \"say $2\"\015'"
  172.  fi
  173. }
  174.  
  175.  
  176. #Start-Stop here
  177. case "$1" in
  178.   start)
  179.     mc_start
  180.     ;;
  181.   stop)
  182.     mc_stop
  183.     ;;
  184.   restart)
  185.     mc_stop
  186.     mc_start
  187.     ;;
  188.   update)
  189.     mc_stop
  190.     mc_backup
  191.     mc_update
  192.     mc_start
  193.     ;;
  194.   backup)
  195.     mc_saveoff
  196.     mc_backup
  197.     mc_saveon
  198.     mc_start
  199.     ;;
  200.   status)
  201.     mc_status
  202.     ;;
  203.   say)
  204.    mc_say
  205.   ;;
  206.   *)
  207.   echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|status|restart|say [message]}"
  208.   exit 1
  209.   ;;
  210. esac
  211.  
  212. exit 0
clone this paste RAW Paste Data