Guest User

Minecraft server init script for Ubuntu/Debian

a guest
Oct 9th, 2010
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.24 KB | None | 0 0
  1. #!/bin/bash
  2. ################################################################################
  3.  
  4. #  This program is free software; you can redistribute it and/or modify it
  5. #  under the terms of the GNU General Public License as published by the
  6. #  Free Software Foundation; either version 2 of the License, or (at
  7. #  your option) any later version.
  8. #
  9. #  This program is distributed in the hope that it will be useful, but
  10. #  WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. #  General Public License for more details.
  13. #
  14. #  You should have received a copy of the GNU General Public License
  15. #  along with this program; if not, write to the Free Software Foundation,
  16. #  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17.  
  18. # Unless you change the below variables, this script assumes:
  19. #  * Only one instance of minecraft is being run
  20. #  * minecraft is installed in /usr/local/minecraft/
  21. #  * The Linux user "mc" exists
  22. #  * The Linux user "mc" owns /usr/local/minecraft/
  23.  
  24. ################################################################################
  25. . /lib/lsb/init-functions
  26.  
  27. ##### You can change these variables #####
  28. MC_PATH=/usr/local/minecraft;
  29. MC_USER=mc;
  30. MEMALOC=768;
  31. MC_JAR=Minecraft_Mod.jar;   # minecraft_server.jar for vanilla, Minecraft_Mod.jar for hey0's mod
  32. MC_LOCK=server.log.lck;
  33. SCREEN_NAME=minecraft;
  34. ID=`which id`;
  35. KILL=`which kill`;
  36. ##### Don't change anything below here #####
  37.  
  38. start_minecraft() {
  39.     log_action_begin_msg "Starting minecraft-server"
  40.  
  41.     # Make sure old minecraft isn't running because they fight for same ip:port
  42.     if [ -f $MC_PATH/$MC_LOCK ]; then
  43.         log_action_end_msg 1    # Fail
  44.         log_failure_msg "Lock file exists. (Is minecraft-server already running?)"
  45.         return
  46.     fi
  47.  
  48.     # Check for existing screen session
  49.     if [ ! "`screen -ls | grep $SCREEN_NAME | wc -l`" == "0" ]; then
  50.         log_action_end_msg 1    # Fail
  51.         log_failure_msg "Screen session exists. (Is minecraft-server already running?)"
  52.         return
  53.     fi
  54.  
  55.     # Allow full core dumps for debugging purposes
  56.     ulimit -c unlimited
  57.  
  58.     # Run as correct user
  59.     if [ "`$ID -u`" == "0" ]; then
  60.         su $MC_USER -c "cd $MC_PATH; screen -m -d -S $SCREEN_NAME java -Xms${MEMALOC}M -Xmx${MEMALOC}M -Djava.net.preferIPv4Stack=true -jar $MC_JAR nogui"
  61.     else
  62.         cd $MC_PATH; screen -m -d -S $SCREEN_NAME java -Xms${MEMALOC}M -Xmx${MEMALOC}M -Djava.net.preferIPv4Stack=true -jar $MC_JAR nogui
  63.     fi
  64.     log_action_end_msg $?
  65. }
  66.  
  67. stop_minecraft() {
  68.  
  69.     log_action_begin_msg "Stopping minecraft-server (nicely)"
  70.     # Verify session exists
  71.     if [ "`screen -ls | grep $SCREEN_NAME | wc -l`" == "0" ]; then
  72.         log_action_end_msg 1    # Fail
  73.         log_failure_msg "Screen session not found. (Is minecraft-server running?)"
  74.         if [ ! "$FORCE" == "TRUE" ]; then
  75.             return
  76.         fi
  77.     else    # Screen reports a session..
  78.         SCREEN_PID=`screen -ls | grep $SCREEN_NAME | cut -f2 | cut -d. -f1`;
  79.         screen -S $SCREEN_NAME -p 0 -X stuff $'say Shutdown requested by console.\n' &>/dev/null
  80.         screen -S $SCREEN_NAME -p 0 -X stuff $'save-all\n' &>/dev/null; sleep 5
  81.         screen -S $SCREEN_NAME -p 0 -X stuff $'stop\n' &>/dev/null; RET=$?; sleep 2
  82.         log_action_end_msg $RET
  83.         if [ ! "$FORCE" == "TRUE" ]; then
  84.             return
  85.         fi
  86.     fi
  87.  
  88.     if [ "$FORCE" == "TRUE" ]; then
  89.         log_action_begin_msg "Stopping minecraft-server (forced)"
  90.         # Use the PID returned by screen, if possible
  91.         if [ -h /proc/${SCREEN_PID-UNSET}/exe ]; then
  92.             $KILL -TERM $SCREEN_PID; sleep 1    # Try to be nice
  93.             $KILL -KILL $SCREEN_PID &>/dev/null # Use more force
  94.         fi
  95.         # Brute force killing
  96.         OTHER_PIDS=`ps ax | grep java | grep $MC_JAR | grep -v grep | cut -d' ' -f2`
  97.         if [ -n "$OTHER_PIDS" ]; then
  98.             $KILL -TERM $OTHER_PIDS; sleep 1    # Try to be nice
  99.             $KILL -KILL $OTHER_PIDS &>/dev/null # Use more force
  100.         fi
  101.  
  102.         # Remove the lock file
  103.         if [ -f $MC_PATH/$MC_LOCK ]; then
  104.             rm -f $MC_PATH/$MC_LOCK
  105.         fi
  106.         log_action_end_msg 0    # Success
  107.     fi
  108.  
  109. }
  110.  
  111.  
  112. if [ ${2-UNSET} == "force" ]; then
  113.     FORCE="TRUE";
  114. fi
  115. case "$1" in
  116.  
  117.     start)
  118.         start_minecraft
  119.         ;;
  120.     stop)
  121.         stop_minecraft
  122.         ;;
  123.     restart)
  124.         stop_minecraft
  125.         log_action_begin_msg "Sleeping"
  126.         sleep 2
  127.         log_action_end_msg $?
  128.         start_minecraft
  129.         ;;
  130.     *)
  131.         echo "usage: $0 {start|stop [force]|restart [force]}"
  132. esac
Advertisement
Add Comment
Please, Sign In to add comment