Linux scripts to help keep your server running, saved, and restarted.

Discussion in 'Bukkit Discussion' started by MacG32, Jun 24, 2011.

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

    MacG32

    I'm providing these scripts without further support and hope they help keep your server running strong. :D You'll need to replace "*user*/*serverdir*" with your login name and server directory's name and "*server variables*" with whatever you start your server with. Enjoy. ;)

    Suggested cron job for chkscr:
    Code:
    * * * * * /home/*user*/*serverdir*/chkscr >/dev/null 2>&1
    chkscr
    Code:
    #!/bin/bash
    # chkscr: Checks if server is running, if not, restarts it
    
    # screen name
    SESSION="server1"
    
    # screen session and exec script
    DAEMON="screen -d -m -S $SESSION /home/*user*/*serverdir*/exec"
    
    # Gets exec's PID
    PROPID=$(ps -aef | grep -v grep | grep '/bin/bash /home/*user*/*serverdir*/exec' | awk '{print $2}')
    
    # Get PID value from file
    read val < /home/*user*/*serverdir*/cb.pid
    
    # Checks if exec's PID and file PID are same, sends message, then exits
    if [ "$PROPID" -eq "$val" ]; then
    # screen -S $SESSION -X exec .! echo 'msg *name* Whatever you'd like said.'
    exit 0
    fi
    
    # If above check fails, deletes PID file
    rm /home/*user*/*serverdir*/cb.pid
    
    # Sends message, restarts server, remakes PID file with exec's PID for above check
    echo "Restarting the server now."
    $DAEMON
    ps -aef | grep -v grep | grep '/bin/bash /home/*user*/*serverdir*/exec' | awk '{print $2}' > /home/*user*/*serverdir*/cb.pid

    exec
    Code:
    #!/bin/bash
    # exec: Starts the server from inside the screen and restarts it, if stopped
    
    while true; do
    java *server variables*
    wait
    done

    Suggested cron job for save:
    Code:
    */10 * * * * /home/*user*/*serverdir*/save >/dev/null 2>&1
    save
    Code:
    #!/bin/bash
    
    screenname=server1
    
    screen -S $screenname -X exec .! echo ' '
    sleep 1
    screen -S $screenname -X exec .! echo 'save-all'
    

    Suggested cron job for reboot:
    Code:
    30 */2 * * * /home/*user*/*serverdir*/save >/dev/null 2>&1
    reboot
    Code:
    #!/bin/bash
    
    screenname=server1
    
    screen -S $screenname -X exec .! echo ' '
    sleep 1
    screen -S $screenname -X exec .! echo 'say Whatever you'd like to say to your players.'
    sleep 10
    screen -S $screenname -X exec .! echo 'save all'
    sleep 1
    screen -S $screenname -X exec .! echo 'stop'
    
     
  2. Offline

    MacG32

    Changed:

    # Get PID value from file
    read val < /home/*user*/*serverdir*/cb.pid

    and

    # If above check fails, deletes PID file
    rm /home/*user*/*serverdir*/cb.pid
     
  3. Offline

    MacG32

    Bump to make sure everyone that needs this has a chance to see and use it. ;)
     
  4. Offline

    Zalastri

    Very nice of you, thanks for your efforts.
     
    MacG32 likes this.
  5. Offline

    MacG32

    Changed:

    ps -aef | grep -v grep | grep '/bin/bash /home/*user*/*serverdir*/exec' | awk '{print $2}' > /home/*user*/*serverdir*/cb.pid

    Another bump to make sure everyone can see and use this, if needed. :)
     
  6. Offline

    admalledd

    for me i use simplecronclone to run my scripts.

    my master backup script:
    Code:
    #!/bin/bash
    echo ---------------->>/tmp/rdiff-log
    echo $(date) >>/tmp/rdiff-log
    
    
    ##backup entire server:
    
    #current server path:
    CUR=$($(dirname $0)/curserverpath.sh)
    
    rdiff-backup -v6 $CUR /home/$USER/minecraft/backup/ >> /tmp/rdiff-log
    
    i use rdiff-backup for incremental saving, logging to /tmp/rdiff-log

    script for current server path:
    Code:
    #!/bin/sh
    ls -la ~/minecraft/ |grep 'cur ->' |awk '{print $NF}'
    
    simple, I use a link to my current server path ( i archive each server update just in case i need to roll back)

    now for my restart script:
    Code:
    #!/bin/bash
    screen_name="server"
    
    function runcmd {
     if [ -n "$STY" ]; then
      screen -x $screen_name -p 0 -X stuff "$1$(echo -ne '\r')"
     else
      screen -p 0 -X stuff "$1$(echo -ne '\r')"
     fi
    }
    #runcmd "say test"
    runcmd "stop"
    sleep 10
    runcmd "./start.sh"
    
    mostly wanted a script to stop and restart simply, and also provide a function to run commands inside the bukkit terminal.
     
    MacG32 likes this.
  7. Offline

    surtic

    MacG32 likes this.
  8. Hi i just found out about this page and was happy to realize others doing all this with crontabs instead of plugins :)
    I am not an expert when it comes to crontabs or linux but i can handle them a bit.

    I was just wondering the crontab that checks if the server is running or not...will it realize if the mc server has stuck?
    I mean not crashed, but a total halt. It happens to me now and then, and if i bring the screen up then i cant type anything in the mc console. Its like its blue screened.(to use an analogy from windows)

    Also i see that you dont use .sh on your scripts. Is that ok ? or should i change everything to chkscr.sh for example.
    I am not that familiar with shell scripts, hence the question.

    Thanks
     
  9. Offline

    admalledd

    hrm.. check to see if the server has hanged? from what I can see, non of the scripts posted thus far do that (if I'm wrong let me know, I'm still learning the art *nix-fu) however, i agree that would be nice to have. some one know how?

    now for the whole .sh thing: linux and friends actually don't look at the file extension when running. you will notice the first line is a bit weird for our scripts? the
    Code:
    #!/bin/bash
    in my scripts? this is used to "interpret" the file using that program. A way to think about it is that the shell (your terminal you type into, normally something similar to bash or sh) reads that first line, then runs the script with that program. example:
    Code:
    $cat test
    #!/bin/sh
    echo "hi"
    $./test
    hi
    $###same as:
    $/bin/sh ./test
    hi
    
    so, because that first line actually tells the computer (really the shell) how to run the program, we don't have to have programs fighting over file extensions nearly as much as windows. however, it does lead to the need for something called "execute permissions" which is really not simple to explain, but thankfully wiki has us covered.

    So, the reason to use the .sh or (for some of mine that i have yet to clean) .py is to make it easier to tell for us users what the file is. linux and friends try to say that a file should be usable with whatever name the user gives it (within reason, don't rename system files if you don't know what they do!)
     
  10. Thats what i thought also :) Thanks for clearing that out.
    Now about the hanging server thingy...it would be nice, yes, to know if that happened from crontab.
    Is there a way the script to be able to get a message back from the server ?
    Sort of like a /ping and the server replies pong...There is a plugin that does such a thing ingame though for the player.

    Any msg would do, like /ver, anything actually, just enough to know that the server replied.
     
  11. Ok its not working for me...maybe cause its CentOS...i dont know...it messes up everything.

    It creates a new screen every minute...doesnt check correctly the pid in the server...its chaos :(
     
  12. Offline

    MacG32

    http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash They are bash scripts, not sh scripts. Also, there are no file extensions. It's really laid out quite simply in the first post. This is one of the many reasons I've stated, " I'm providing these scripts without further support..." I spent about a week and a half perfecting those scripts for my needs and didn't want anyone else having to go through what I did. I would suggest to anyone using those scripts, use them exactly the way you see them and in the exact ways stated. Then, when they're working for you, go ahead and alter them to your liking. If you don't understand what you're looking at or how to do what is stated, Google is your friend and will answer those questions effortlessly. Good luck and have fun. ;)
     
  13. Ok i figured out the bash / sh mix up.
    I still get the same error running chkscr, but it runs. What worries me is that i cant stop the server.lol
    I type /stop and it just restarts its.
    Is it suppose to do that ? ( i am not talking about crontab running chkscr). I run chkscr from console myself.
     
  14. Offline

    MacG32

    Two errors should display when running for the first time or starting the server from a complete stop, because the cb.pid doesn't exist, there's no value to read, and/or the value in cb.pid isn't correct, but it does state, "Restarting the server now." I didn't throw in any error handling. It's designed to keep your server running 24/7. To stop your server and keep it from restarting, you'll have to disable the exec cron job, save-all in the screen, and kill -9 this process: /bin/bash /home/*user*/*serverdir*/exec . While the server is running, you can alter, rename, update, and delete plugins and CraftBukkit, then type save-all and stop in the screen. My update process is renaming plugins and/or CraftBukkit by adding -bak to the end of the file(s) to be updated, copying over the updates, copying the server.log to my PC, deleting the log on the server, then save-all, stop, it auto-restarts, then read the new log for any errors. If there are any errors, delete the error causing update, remove the -bak from the old version, save-all, stop, it auto-restarts, then check the log again. Hope that explains it.
     
Thread Status:
Not open for further replies.

Share This Page