#!/bin/bash # /etc/init.d/gameserver # version 0.1b 9MAR2015 ### BEGIN INIT INFO # Provides: gameserver # 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: CustomGameServer # Description: Starts the custom game server ### END INIT INFO #Settings JARFILE='cauldron-1.7.10-1.1307.06.218-server.jar' USERNAME="minecraft" GAMESTORE="/var/www/html/game" GAMEPATH="/dev/shm/game" BACKUPPATH="/var/www/html/game_backup" CPU_COUNT=1 INVOCATION="java -Xmx3072M -Xms3072M -server -jar $JARFILE -o false" WORLD=rr3world #DONT EDIT BELOW THIS LINE #SPECIAL CHARACTER CODES ENTER="`echo -ne '\015'`" to_screen() { #TO_SCREEN="screen -p 0 -S gameserver -X eval 'stuff " screen -S gameserver -p 0 -X stuff "$1" } to_screenNL() { #TO_SCREEN="screen -p 0 -S gameserver -X eval 'stuff " screen -S gameserver -p 0 -X stuff "$1$ENTER" } game_status() { ps aux |grep -F -v grep|grep -F -v SCREEN|grep -F --quiet $JARFILE return $? } game_start() { if [ -d $GAMESTORE/$WORLD.bak ]; then echo "last $WORLD.bak still exist, crashed warning! manual check required!!!" exit 1 fi cd $MCPATH if [ ! -f "$GAMEPATH/$JARFILE" ]; then mkdir /dev/shm/game cp -R $GAMESTORE/* $GAMEPATH/ chmod -R 777 $GAMEPATH fi to_screenNL "exit" screen -dmS gameserver to_screenNL "cd $GAMEPATH" to_screenNL "$INVOCATION" sleep 7 if game_status; then echo "$JARFILE : running." else echo "Could not start : $JARFILE?" sleep 13 if game_status; then echo "$JARFILE : running finally." else echo "Could not start : $JARFILE after waiting 20 seconds" fi fi } game_stop() { echo "...prepare to be stopped" to_screenNL "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING" to_screenNL "save-all" echo "... 35 seconds left - save complete" sleep 5 to_screenNL "stop" echo "... 20 seconds left - sent stop command" sleep 10 to_screenNL "exit" echo "... 10 seconds left - sent exit command for terminal" sleep 10 } #Start-Stop here case "$1" in status) if game_status; then echo "$JARFILE is running." else echo "$JARFILE is not running." fi ;; start) if game_status; then echo "$JARFILE was already running" else game_start fi ;; stop) if game_status; then echo "$JARFILE is running..." game_stop if game_status; then echo "$JARFILE could not be shut down... check in 20 seconds?" sleep 20 if game_status; then echo "$JARFILE could not be shut down... restart system?" else echo "$JARFILE finally shut down... lots of lag? upgrade your system." fi else echo "$JARFILE is shut down." fi else echo "$JARFILE was already not running." fi ;; exek) echo "exec - $2" $2 ;; help) echo " start - start server" echo " stop - stop server" echo " status - if server running or not" echo " check - if ramdisk game directory empty or not" echo " build - copy game-folder from STORE to SHM" echo " stabalize - delete STORE_1" echo " rename STORE to STORE_1" echo " MOVE game-folder from SHM to STORE" echo " fsave - force save" echo " command - send command to server" echo " exec - use only for advanced debugging" ;; *) echo "Usage: /etc/init.d/gameserver {help|start|stop|status|check|build|stabalize|fsave|command|exec}" exit 1 ;; esac