Tutorials/Server startup script

This is an example of possible Minecraft server startup and maintenance script for debian based distros.

Script

 * 1) ! /bin/sh
 * 2) /etc/init.d/minecraft

SERVICE='minecraft_server.jar' USERNAME="user" MCPATH='/home/user/MC'
 * 1) Settings

mc_start { if ps ax | grep -v grep | grep $SERVICE > /dev/null then echo "Tried to start but $SERVICE was allready running!" else echo "$SERVICE was not running... starting." cd $MCPATH su - $USERNAME -c "cd $MCPATH && screen -dmS minecraft java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui" sleep 7 if ps ax | grep -v grep | grep $SERVICE > /dev/null then echo "$SERVICE is now running." else echo "Could not start $SERVICE." fi       fi }

mc_stop { if ps ax | grep -v grep | grep $SERVICE > /dev/null then echo "$SERVICE is running... stopping." su - $USERNAME -c "screen -p 0 -S minecraft -X stuff "stop"" su - $USERNAME -c "screen -p 0 -S minecraft -X stuff `echo '\015'`" sleep 7 else echo "$SERVICE was not running." fi       if ps ax | grep -v grep | 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 $SERVICE > /dev/null then echo "$SERVICE is running! Will not start update." else su - $USERNAME -c "cd $MCPATH && cp -r world world_`date "+%d.%m.%Y"`" su - $USERNAME -c "cd $MCPATH && mv minecraft_server.jar "minecraft_server_`date "+%d.%m.%Y"`.jar"" su - $USERNAME -c "cd $MCPATH && wget http://www.minecraft.net/download/minecraft_server.jar?v=1289500485639" su - $USERNAME -c "cd $MCPATH && mv minecraft_server.jar?v=1289500485639 minecraft_server.jar" fi

} case "$1" in start)    mc_start    ;;  stop) mc_stop ;;
 * 1) Start-Stop here

restart)   mc_stop    mc_start    ;;

update)   mc_stop    mc_update    mc_start    ;; status) if ps ax | grep -v grep | grep $SERVICE > /dev/null then echo "$SERVICE is running." else echo "$SERVICE is not running." fi ;;

*)   echo "Usage: /etc/init.d/minecraft {start|stop|update|status|restart}"    exit 1    ;; esac

exit 0

Requirements

 * Debian based linux distro.


 * screen

Installation
Use your favorite editor to create file called minecraft in /etc/init.d/ and paste the script above in that file.

Edit the USERNAME and MCPATH -variables according to your setup.

Make sure the newly created file has required permissions You can set the permissions by running: chmod 755 /etc/init.d/minecraft

Then run update-rc.d minecraft defaults to add required symbolic links. Note: your system will most likely warn you that the script does not meet all requirements. The script will however work.

Uninstall
update-rc.d -f minecraft remove

Usage
/etc/init.d/minecraft start stop, restart or update