[question]server restart

Discussion in 'Bukkit Help' started by aidan matzko, Apr 28, 2011.

Thread Status:
Not open for further replies.
  1. Hey, i was just wondering if there is a way to set my server to make a backup(by backupplugin(the fork)) and then restart.
    I am on Ubuntu 10.10, and this really isn't urgent. would it be something in the .sh? or is this more a plugin?
     
  2. Offline

    mughi

    there may be plugins that do this directly.. or you could find one of the tcp control plugins and use scripts to fire off commands that way
     
  3. just wondering, if i modified the .sh, wouldn't that work?
     
  4. Offline

    mughi

    REALLY depends on what you change :)

    sorry, i'm not sure i know what you are thinking of.
     
  5. i want to shutdown the server, and then start it again. so, what im thinking is in the .sh
    start the server
    backup world
    shutdown server and start new .sh
    close .sh

    would that maybe work?
     
  6. Offline

    mughi

    not likely..

    process goes:

    server start (sh WAITS)
    world backs up
    server continues running, sh continues waiting

    you manually stop server..
    sh resumes, tries to shutdown server (how would you even do that), starts a new server and tries to close itself, taking server with it.
     
  7. hmm. that is a problem :D
     
  8. Offline

    mughi

    BUT:

    if you got the TCP commands plugin and the backup plugin:

    sh has command to start server, as always

    separate .sh has command to STOP server via tcp command, then restart server..you'd have to use screen for that
    separate .sh is run via cron job (scheduled task)

    pretty sure others have done this before, try searching for cron on the forums
     
  9. ok. what is TCP commands plugin?
     
  10. Offline

    serenewaffles

  11. thanks, im gonna post the code i edited here, so someone can check it over :D
    i may delete the parts i don't need...
     
  12. Offline

    JWhy

  13. alright, so how does this look? this was just really quick, im sure there are some errors ;)
    Code:
    #!/bin/bash
    # /etc/init.d/minecraft
    # version 0.3.2 2011-01-27 (YYYY-MM-DD)
    
    ### 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:    Starts the minecraft server
    ### END INIT INFO
    
    #Settings
    SERVICE='craftbukkit.jar'
    USERNAME="minecraft"
    MCPATH='/home/minecraft/minecraft'
    CPU_COUNT=2
    INVOCATION="java -Xmx3G -Xms3G -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar craftbukkit.jar nogui"
    BACKUPPATH='/media/remote.share/minecraft.backup'
    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_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 \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
                    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
    }
    
    mc_backup() {
       echo "Backing up minecraft world"
       if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"` ]
       then
         for i in 1 2 3 4 5 6
         do
           if [ -d $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i ]
           then
             continue
           else
             as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`-$i"
             break
           fi
         done
       else
         as_user "cd $MCPATH && cp -r world $BACKUPPATH/world_`date "+%Y.%m.%d"`"
         echo "Backed up world"
       fi
       echo "Backing up the minecraft server executable"
       if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar" ]
       then
         for i in 1 2 3 4 5 6
         do
           if [ -f "$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar" ]
           then
             continue
           else
             as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`-$i.jar\""
             break
           fi
         done
       else
         as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/minecraft_server_`date "+%Y.%m.%d"`.jar\""
       fi
       echo "Backup complete"
    }
     
    #Start-Stop here
    case "$1" in
      start)
        mc_start
        ;;
      stop)
        mc_stop
        ;;
      restart)
        mc_stop
        mc_start
        ;;
      restart)
        mc_backup
        mc_stop
        mc_start
      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|update|backup|status|restart}"
      exit 1
      ;;
    esac
    
    exit 0
     
  14. Offline

    serenewaffles

    Looks good. You don't want to remove the as_user() method/function because it lets you run commands as the minecraft user without being logged in as the minecraft user. Also, every command the script executes goes through as_user(). Other than that, it is pretty good out of the box, you only have to change anything under #Settings to set it up.

    Just make sure it is executable and you ran the commands to update rc.d. I don't know what user name you use to run the server, but that's what you want for USERNAME. Otherwise it won't have the right permissions. Likewise, you have to set up your directories so that they point in the right direction.
     
Thread Status:
Not open for further replies.

Share This Page