initscript help -- restart problem since 1.6.2 update

Discussion in 'Bukkit Help' started by zolcos, Sep 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    zolcos

    My initscript has a "log roll" feature getting called nightly by cron where it stops the server, rotates the log file, then starts the server up again. This has worked fine for over a year but since I updated to the RB (1.6.2-R1.0), the server stays down after rolling the log. I found no errors in the bukkit log or in the system log. When I notice it in the morning, the initscript correctly reports the status as "stopped" (not crashed) and it comes right up when I do "/etc/init.d/minecraft start"
    The server uses Gentoo Linux x64 and vixie-cron.
    This is my initscript:
    Code:
    #!/bin/bash
    # /etc/init.d/minecraft
     
    ### BEGIN INIT INFO
    # Provides:  minecraft
    # 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:    Minecraft server
    # Description:    Init script for minecraft/bukkit server, with rolling logs and use of ramdisk for less lag.
    ### END INIT INFO
     
    # Name of server.jar file
    SERVICE='craftbukkit.jar'
    # User that should run the server
    USERNAME="minecraft"
    # Path to minecraft directory
    MCPATH='/var/lib/minecraft/server'
    CPU_COUNT=12
    # Change the values of -Xmx###M and -Xms###M to how mutch ram you want to give to the
    # server, remember to make room for the ramdisk
    INVOCATION="java -Xmx4G -Xms1G -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE nogui"
    # Where the world backups should go
    BACKUPPATH='var/lib/minecraft/backups'
    # Where the logs are copied when running log-roll
    LOGPATH="/var/lib/minecraft/logs"
    # Where the whole minecraft directory is copied when whole-backup is runned
    WHOLEBACKUP='/var/lib/minecraft/wholebackups'
    # Where the world is located on the disk
    WORLDSTORAGE="${MCPATH}/diskworld"
    #Path to the the mounted ramdisk default in ubuntu: /dev/shm
    RAMDISK='/dev/shm'
    #List of MySQL databases to include in "wholebackup" operation
    DATABASES='commandshops pex'
     
    ME=`whoami`
    as_user() {
        if [ $ME == $USERNAME ] ; then
            bash -c "$1"
        else
            su - $USERNAME -c "$1"
        fi
    }
    datepath() {
        # datepath path filending-to-check returned-filending
        if [ -e $1`date +%x`$2 ]
        then
            echo $1`date +%FT%T`$3
        else
            echo $1`date +%F`$3
        fi
    }
    mc_start() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
            echo "Tried to start but $SERVICE was already running!"
        else
            echo "$SERVICE was not running... starting."
            cd $MCPATH
            as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
            sleep 7
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                echo "$SERVICE is now running."
            else
                echo "Could not start $SERVICE."
            fi
        fi
    }
     
    mc_saveoff() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
            echo "$SERVICE is running... suspending saves"
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
            sync
            sleep 10
        else
            echo "$SERVICE was not running. Not suspending saves."
        fi
    }
     
    mc_saveon() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
            echo "$SERVICE is running... re-enabling saves"
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
        else
            echo "$SERVICE was not running. Not resuming saves."
        fi
    }
     
    mc_stop() {
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
            echo "$SERVICE is running... stopping."
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
            sleep 10
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
            sleep 7
        else
            echo "$SERVICE was not running."
        fi
        if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
        then
            echo "$SERVICE could not be shut down... still running."
        else
            echo "$SERVICE is shut down."
        fi
    }
    log_roll() {
        path=`datepath $LOGPATH/server_ .log.gz .log`
        as_user "cp $MCPATH/server.log $path && gzip $path"
        as_user "truncate $MCPATH/server.log --size 0"
    }
    mc_whole_backup() {
        echo "Backing up FS data..."
        path=`datepath $WHOLEBACKUP/mine_`
        as_user "mkdir $path"
        as_user "cp -r $MCPATH $path/server"
        find $WHOLEBACKUP -maxdepth 1 -type d -mtime +30 -exec rm -r {} \;
        echo "Backing up databases..."
        as_user "mysqldump --no-create-db --no-create-info --no-tablespaces --databases $DATABASES >$path/databases.sql"
        echo "Wholebackup done."
    }
    mc_world_backup() {
        echo "Backing up minecraft world..."
        path=`datepath $BACKUPPATH/world_ .tar.bz2 .tar.bz2`
        as_user "tar -hcjf $path $WORLDSTORAGE"
        find $BACKUPPATH -maxdepth 1 -type d -mtime +30 -exec rm -r {} \;
        echo "World backup done."
    }
    to_ram() {
        if [ -L $MCPATH/world ]
        then
            echo "Copying world to ram..."
            mkdir $RAMDISK/minecraft
            chown -R $USERNAME:root $RAMDISK/minecraft
            as_user "rsync -rt $WORLDSTORAGE/ $RAMDISK/minecraft"
            echo "World copied to ram."
        fi
    }
    to_disk() {
        echo "Copying world to disk..."
        as_user "rsync -rt $RAMDISK/minecraft/* $WORLDSTORAGE"
        echo "World copied to disk."
    }
     
    case "$1" in
        start)
            # Starts the server
            to_ram
            mc_start
            ;;
        stop)
            # Stops the server
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
            mc_stop
            to_disk
            ;;
        restart)
            # Restarts the server
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
            mc_stop
            mc_start
            ;;
        backup)
            # Backups world
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"say Backing up world.\"\015'"
            mc_saveoff
            mc_world_backup
            mc_saveon
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"say Backup complete.\"\015'"
            ;;
        whole-backup)
            # Backups everything
            mc_whole_backup
            ;;
        to-disk)
            # Writes from the ramdisk to disk, in case the server crashes.
            # Using ramdisk speeds things up alot, especially if you allow
            # teleportation on the server.
            mc_saveoff
            to_disk
            mc_saveon
            ;;
        connected)
            # Lists connected users
            as_user "screen -p 0 -S minecraft -X eval 'stuff list\015'"
            sleep 3s
            tac $MCPATH/server.log | grep -m 1 "Connected"
            ;;
        log-roll)
            # Moves and Gzips the logfile, a big log file slows down the
            # server ALOT (what was notch thinking?)
            as_user "screen -p 0 -S minecraft -X eval 'stuff \"say ROUTINE REBOOT IN 10 SECONDS.\"\015'"
            mc_stop
            log_roll
            mc_start
            ;;
        status)
            # Shows server status
            if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
            then
                echo "$SERVICE is running."
            else
                echo "$SERVICE is not running."
            fi
            ;;
     
        *)
            echo "Usage: /etc/init.d/minecraft {start|stop|backup|whole-backup|log-roll|to-disk|connected|status|restart}"
            exit 1
            ;;
    esac
     
    exit 0
    
    and my crontab:
    Code:
    #min  hr    day    month  weekday
    20    01    *      *      *        /etc/init.d/minecraft log-roll
    02    01    */2    *      *        /etc/init.d/minecraft whole-backup
    */30  *      *      *      *        /etc/init.d/minecraft to-disk
     
  2. Offline

    Lolmewn Retired Staff

    Is this the one from github? Maybe it has an update.. Other than that, it looks fine to me. Your craftbukkit file name didn't change either?
     
  3. Offline

    zolcos

    This is derived from one I found on the minecraft wiki which is probably related to the one on github. I copied it years ago and have been doing my own updates to it like adding support for the nether.
    I noticed the restart worked fine last night, but the one that just happened failed again. It might be only failing on nights where the backup is run. Now that I think about it, there was one other thing that changed besides updating to 1.6.2 -- I edited this script to add mysqldump to the 'wholebackup' operation. But the backups are successful and it never gives problems when I run it manually.
     
  4. Offline

    PolarCraft

    Make sure to check the github. Just to double check.
     
Thread Status:
Not open for further replies.

Share This Page