Auto update script for BungeeCord in tmux

Discussion in 'Server & Community Management' started by Datatags, May 7, 2017.

  1. I made a shell script to auto update the spigot servers and BungeeCord, all managed by tmux. I hope it helps somebody. Modified this to a more general setup than mine, so not 100% sure that it works perfectly. It should though. I added some variables at the top that you will need to set. I'm also new to shell scripting, so it probably isn't that great. Hope it works for you! :D

    Code (Bash):
    #!/bin/bash
    # Build new server jar, download BungeeCord jar, display a warning message, shut down network, copy new jars to the appropriate folders, bring network back online.

    $minecraftdir="~/Documents/MCserver" # Directory containing spigot servers
    $buildtoolsdir="~/Downloads/BuildTools/" # Directory where your BuildTools is
    $bungeecorddir="~/Documents/MCserver/BungeeCord/" # Directory where your BungeeCord jar is located
    echo Building Spigot, please wait...
    cd $buildtoolsdir
    java -jar BuildTools.jar --rev 1.11.2 # Build spigot jar
    cd $bungeecorddir # Go to the BungeeCord dir to download new
    echo Downloading BungeeCord...
    wget -O BungeeCord-fresh.jar https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar # Download latest BungeeCord build
    cd $minecraftdir # Go to minecraft directory
    echo Making announcements.  The update will begin in 5 minutes.
    tmux select-window -t 0 # Make sure the BungeeCord window is selected.  If your BungeeCord window isn't 0, fix it here.  Also be sure to fix it near the end of the script when it starts BungeeCord again.
    tmux send-keys 'alert This is an automated message:  Server shutting down for update in 5 minutes.  The update will take about 2 minutes.' Enter # Broadcast shutdown notice
    sleep 240 # Wait
    echo T minus 1 minute
    tmux send-keys 'alert This is an automated message:  Server shutting down for update in 1 minute.' Enter # Repeat
    sleep 30
    echo T minus 30 seconds
    tmux send-keys 'alert This is an automated message:  Server shutting down for update in 30 seconds.' Enter # Repeat
    sleep 20
    echo T minus 10 seconds
    tmux send-keys 'alert This is an automated message:  Server shutting down for update in 10 seconds.' Enter # Repeat
    sleep 10
    echo Shutting down network...
    tmux send-keys 'end end' Enter # Finally terminate BungeeCord.  I don't know why, but it does not seem to work unless I write end twice.  send-keys 'end' sends a blank command to Bungee, send-keys 'end end' sends "end end" which terminates it.  :?  Please post if you know why this is!
    sleep 1 # Wait for it to finish up

    for i in 1 2 3 4 5 6 7 8 ; do # Loop through all the server instances.  Fix here if you have more or less.
    tmux select-window -t $i # Select the correct window in order.
    tmux send-keys 'stop' Enter # Terminate the server
    sleep 2 # Give it two seconds to shut down
    done

    echo Copying files...
    mv $bungeecorddir/BungeeCord-fresh.jar $bungeecorddir/BungeeCord.jar # Replace old BungeeCord.jar with BungeeCord-fresh.jar
    cp $buildtoolsdir/spigot-1.11.2.jar . # Copy newly built Spigot jar file into current directory ($minecraftdir) for less typing
    cp ./spigot-1.11.2.jar server1/ # Copy to server folder 1
    cp ./spigot-1.11.2.jar server2/ # Copy to server folder 2
    cp ./spigot-1.11.2.jar server3/ # etc.
    cp ./spigot-1.11.2.jar server4/
    cp ./spigot-1.11.2.jar server5/
    cp ./spigot-1.11.2.jar server6/
    cp ./spigot-1.11.2.jar server7/
    cp ./spigot-1.11.2.jar server8/
    rm ./spigot-1.11.2.jar # Delete the jar file from the current directory

    echo Bringing servers 1 - 4 back online...  # I like to start the servers four at a time.  My PC probably isn't that great, so it takes 90 seconds max for all four to start up.
    for i in 1 2 3 4 ; do # Loop through all the server instances again,
    tmux select-window -t $i # Select the correct window
    tmux send-keys 'java -jar spigot-1.11.2.jar' Enter # Start the server
    sleep 2 # Give it two seconds to... do something.  I like to sleep 1 or 2 seconds before sending another command, just because.  Remove this if you think that's silly.
    done
    echo Giving servers time to start up...
    sleep 90
    echo Bringing servers 5 - 8 back online... # See above.
    for i in 5 6 7 8 ; do
    tmux select-window -t $i
    tmux send-keys './start.sh' Enter
    sleep 2
    done

    echo Bringing BungeeCord back online...  # Do this last so that it starts once all the other servers are online.  You could also modify the order, like start your four or five most popular servers, then BungeeCord, then your less popular ones.
    tmux select-window -t 0 # Switch to BungeeCord window.
    tmux send-keys 'java -jar BungeeCord.jar' Enter # Start BungeeCord

    echo Update complete! # Echo success!
     
     
  2. Looks way more complicated than it needs to be.

    I personally build the jar first, then copy it immediately. After that its sending the restart command to the wrapper door each server and I'm done. I only have a couple servers, but if I needed to I could delay the restart command as necessary. That way the server is only down for as long as a generic restart takes, not up to 2 minutes
     
  3. can you share your script, please? I'd like to do comparison.
     
  4. I never was happy with the way the restart command worked, because it always ended up not attached to the terminal the previous one was in. How did you resolve that? Or did I misunderstand you?
     
  5. You did. I wanted to see @ryans1230 script.
    @AlanZ I'll try to re-create a working script taking yours as an example. I hope it will turn out working fine.
     
  6. By the way @AlanZ, do you know how to loop this script correctly?
     
  7. I use MinecraftRemoteToolkit, which I've patched for 1.11
     
  8. Probably not. Is it looping incorrectly?
     
  9. I have no clue how to loop it besides using cronjob which does not seem to be working for me.
     
  10. Okay now I'm confused. You mean loop as in have it fire every week or whatever time period? If so, that's what I use and it works great for half the scripts I have in it and not at all for the other half. Probably just something I did wrong. :p
     
  11. I meant like repeat after certain time. Everything works flawlessly now, I used cronjob. Thanks.