Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ################################################################################
- # This program is free software; you can redistribute it and/or modify it
- # under the terms of the GNU General Public License as published by the
- # Free Software Foundation; either version 2 of the License, or (at
- # your option) any later version.
- #
- # This program is distributed in the hope that it will be useful, but
- # WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software Foundation,
- # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- # Unless you change the below variables, this script assumes:
- # * Only one instance of minecraft is being run
- # * minecraft is installed in /usr/local/minecraft/
- # * The Linux user "mc" exists
- # * The Linux user "mc" owns /usr/local/minecraft/
- ################################################################################
- . /lib/lsb/init-functions
- ##### You can change these variables #####
- MC_PATH=/usr/local/minecraft;
- MC_USER=mc;
- MEMALOC=768;
- MC_JAR=Minecraft_Mod.jar; # minecraft_server.jar for vanilla, Minecraft_Mod.jar for hey0's mod
- MC_LOCK=server.log.lck;
- SCREEN_NAME=minecraft;
- ID=`which id`;
- KILL=`which kill`;
- ##### Don't change anything below here #####
- start_minecraft() {
- log_action_begin_msg "Starting minecraft-server"
- # Make sure old minecraft isn't running because they fight for same ip:port
- if [ -f $MC_PATH/$MC_LOCK ]; then
- log_action_end_msg 1 # Fail
- log_failure_msg "Lock file exists. (Is minecraft-server already running?)"
- return
- fi
- # Check for existing screen session
- if [ ! "`screen -ls | grep $SCREEN_NAME | wc -l`" == "0" ]; then
- log_action_end_msg 1 # Fail
- log_failure_msg "Screen session exists. (Is minecraft-server already running?)"
- return
- fi
- # Allow full core dumps for debugging purposes
- ulimit -c unlimited
- # Run as correct user
- if [ "`$ID -u`" == "0" ]; then
- 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"
- else
- cd $MC_PATH; screen -m -d -S $SCREEN_NAME java -Xms${MEMALOC}M -Xmx${MEMALOC}M -Djava.net.preferIPv4Stack=true -jar $MC_JAR nogui
- fi
- log_action_end_msg $?
- }
- stop_minecraft() {
- log_action_begin_msg "Stopping minecraft-server (nicely)"
- # Verify session exists
- if [ "`screen -ls | grep $SCREEN_NAME | wc -l`" == "0" ]; then
- log_action_end_msg 1 # Fail
- log_failure_msg "Screen session not found. (Is minecraft-server running?)"
- if [ ! "$FORCE" == "TRUE" ]; then
- return
- fi
- else # Screen reports a session..
- SCREEN_PID=`screen -ls | grep $SCREEN_NAME | cut -f2 | cut -d. -f1`;
- screen -S $SCREEN_NAME -p 0 -X stuff $'say Shutdown requested by console.\n' &>/dev/null
- screen -S $SCREEN_NAME -p 0 -X stuff $'save-all\n' &>/dev/null; sleep 5
- screen -S $SCREEN_NAME -p 0 -X stuff $'stop\n' &>/dev/null; RET=$?; sleep 2
- log_action_end_msg $RET
- if [ ! "$FORCE" == "TRUE" ]; then
- return
- fi
- fi
- if [ "$FORCE" == "TRUE" ]; then
- log_action_begin_msg "Stopping minecraft-server (forced)"
- # Use the PID returned by screen, if possible
- if [ -h /proc/${SCREEN_PID-UNSET}/exe ]; then
- $KILL -TERM $SCREEN_PID; sleep 1 # Try to be nice
- $KILL -KILL $SCREEN_PID &>/dev/null # Use more force
- fi
- # Brute force killing
- OTHER_PIDS=`ps ax | grep java | grep $MC_JAR | grep -v grep | cut -d' ' -f2`
- if [ -n "$OTHER_PIDS" ]; then
- $KILL -TERM $OTHER_PIDS; sleep 1 # Try to be nice
- $KILL -KILL $OTHER_PIDS &>/dev/null # Use more force
- fi
- # Remove the lock file
- if [ -f $MC_PATH/$MC_LOCK ]; then
- rm -f $MC_PATH/$MC_LOCK
- fi
- log_action_end_msg 0 # Success
- fi
- }
- if [ ${2-UNSET} == "force" ]; then
- FORCE="TRUE";
- fi
- case "$1" in
- start)
- start_minecraft
- ;;
- stop)
- stop_minecraft
- ;;
- restart)
- stop_minecraft
- log_action_begin_msg "Sleeping"
- sleep 2
- log_action_end_msg $?
- start_minecraft
- ;;
- *)
- echo "usage: $0 {start|stop [force]|restart [force]}"
- esac
Advertisement
Add Comment
Please, Sign In to add comment