[URGENT] VPS- script-auto restart all my servers

Discussion in 'Systems Administration' started by gamerstar62, Feb 20, 2016.

  1. Hello,(Sorry for my English, i'm French)

    I search a script for restart all my Minecraft servers after a crash of my VPS. I found a script but he doesn't work...This script have to restart my Bungeecord, Lobby, AuthServeur, and my SkyBlock.

    Some informations :
    My script:
    Code (Text):
    #!/usr/bin/env bash

    ##
    ## This script was created to start/stop/restart servers in screen sessions for Minecraft. You must edit the
    ## servers list and servers_directory to reflect your setup. You also need to have a start.sh for every server to start
    ## them with the options you want.
    ##

    servers=("Proxy" "Lobby" "AuthServeur" "SkyBlock" "HG-Egypte-1")
    servers_directory=(/home/BC-BattleCube/) ### Must include trailing slash

    restart_all() {
        for server in "${servers[@]}"
        do
            echo "$server"
            if screen -list | grep -q "$server"; then
                echo "Stopping $server server.."
                screen -S "$server" -X stuff "msgall  &9Server is restarting...$(printf \\r)"
                screen -S "$server" -X stuff "stop$(printf \\r)"
                echo "$server server has stopped.."
            fi
            echo "Waiting 5 seconds for all servers to stop gracefully..."
            sleep 5
            cd "$servers_directory$server"
            echo "Starting $server server.."
            screen -dmS "$server" ./start.sh
        done
    }
    start_all() {
        for server in "${servers[@]}"
        do
            if screen -list | grep -q "$server"; then
                echo "$server is already running! Skipping... "
            else
                echo "Starting $server server..."
                cd "$servers_directory$server"
                screen -dmS "$server" ./start.sh
            fi
        done
        echo "All servers started"
    }
    stop_all() {
        for server in "${servers[@]}"
        do
            echo "Stopping $server server.."
            screen -S "$server" -X stuff "msgall  &9Server is stopping...$(printf \\r)"
            screen -S "$server" -X stuff "stop$(printf \\r)"
            echo "$server server has stopped.."
        done
    }
    start() {
        if [[ " ${servers[@]} " =~ "$1" ]]; then
            if screen -list | grep -q "$1"; then
                echo "$1 is already running! Skipping... "
            else
                echo "Starting $1 server..."
                cd "$servers_directory$1"
                screen -dmS "$1" ./start.sh
            fi
        else
            echo "Server isn't in the list ~ use: start SERVERNAME"
        fi
    }
    stop() {
        if [[ " ${servers[@]} " =~ "$1" ]]; then
            screen -S "$1" -X stuff "msgall  &9Server is stopping...$(printf \\r)"
            screen -S "$1" -X stuff "stop$(printf \\r)"
            echo "$! server has stopped.."
        else
            echo "Server isn't in the list ~ use: stop SERVERNAME"
        fi
    }
    restart() {
        if [[ " ${servers[@]} " =~ "$1" ]]; then
            if screen -list | grep -q "$1"; then
                echo "Stopping "$!" server.."
                screen -S "$1" -X stuff "msgall  &9Server is restarting...$(printf \\r)"
                screen -S "$1" -X stuff "stop$(printf \\r)"
                echo "$1 server has stopped.."
            fi
            echo "Waiting 5 seconds for $1 server to stop gracefully..."
            sleep 5
            cd "$servers_directory/$1"
            echo "Starting $1 server.."
            screen -dmS "$1" ./start.sh
        else
            echo "Server isn't in the list ~ use: restart SERVERNAME"
        fi

    }
    case "$1" in
        start-all)
            echo "Starting all servers"
            start_all
            ;;
        stop-all)
            echo "Stopping all servers"
            stop_all
            ;;
        restart-all)
            echo "Restarting all servers"
            restart_all
            ;;
        start)
            echo "Attempting to start server"
            start $2
            ;;
        stop)
            echo "Attempting to stop server"
            stop $2
            ;;
        restart)
            echo "Attempting to restart server"
            restart $2
            ;;
        *)
            echo "No command specificed restarting all servers!"
            restart_all
            exit 1
    esac
    exit 0
    The location of my servers:
    [​IMG]

    Thank's for your help ;)
     
  2. I created that script. What error are you getting?
     
  3. Syntax error : "(" unexpected
    (At line 9)
    I name this .sh go.sh and i put it in my home folder. I run with Debian 8 and Java 8.

    Thank's for your help and your script ;)
     
  4. It's a bash script, not shell script. How are you starting the script? You don't need to name it .sh
     
  5. I am a newbie un linux ^^ don't laugh but i use "sh go.sh start-all" for use your script
     
  6. How i name your script ? go.txt ? go.yml?

    Thank's for your help ;)
     
  7. I thought so. Don't use sh to start it. If you've named it go.sh just run this.

    Code (Text):
    ./go.sh
     
  8. You can actually name it anything you want. The first line will tell linux how to run it. When I set it up for people I just name it
    Code (Text):
    manager
     
  9. And some people tell me to set CRON with like that:
    "crontab -e"
    And add at the end of this file that:
    "@reboot sh /home/go.sh start-all"

    That true?

    Thank's for your help :)
     
  10. That should work, yes.
     
  11. Little problem ^^
    -bash: crontab: command not found
    How i can fix that?

    Thank's
     
  12. install crontab I guess
     
  13. I search ho to installer it on Google ;) but if you have the command to install it you give me this command please ;)
    Edit:
    I found ;)
    apt-get update
    apt-get install cron
     
    #13 gamerstar62, Feb 20, 2016
    Last edited: Feb 20, 2016
  14. Where did you get your debian/ubuntu install from that didn't have cron?
     
  15. That my host who auto install Debian when i buy my vps and i found some folders who have the name Cron.daily CRON.weekly...
    But when i did crontab -e that doesn't work so i install it ^^ with apt-get install CRON, i know that strange maybe my host don't really install CRON... ^^
     
  16. If I want to start one server i only use "start lobby" or is use "go.sh start lobby" ?
     
  17. Problem please the command run but she doesn't do anythings
    Code (Text):

    [email protected]*******:/home# ./go.sh start HG-Egypte-1
    Attempting to start server
    Starting HG-Egypte-1 server...
     
    We saw he want to start HG-Egypte-1 but in-game this doesn't start

    Thank's for your help :)

    Edit:

    I forgot to set my start.sh in CHOMD 777

    Thank's for your help @Clinton !
     
  18. You're welcome, but I don't suggest setting permissions to 777!
     
  19. Yes but I remembered I had read it had to put in 777 the script to execute him with another script ^^, so what chmod did you suggest?. You do a wonderful script thank's you ;)
     
  20. Whoever told you that doesn't know what they're doing. It only needs to be executable for the owner. You're welcome.