Recommended Posts

BakaSchwarz    30

Hello everyone!

Some time ago i started working on a script that fully automates the procedure of running and updating a server.

The following script is my as of now final result (fixes may occur if needed). It does the following in its current configuration:

  1. Update the game
  2. Start the Caves
  3. Start the Master
  4. Wait for 20 minutes
  5. Check for an update. If an update is
    • not needed, go to 4.
    • is needed, go to 6.
  6. Announce on the server that an update is needed and that the server will restart in 15 minutes
    • Repeat the warning 5 and 1 minute/s before the shutdown so every player can plan ahead
  7. Shut down the Caves
  8. Shut down the Master
  9. Go to 1.

You can also force a shutdown using the Ctrl + C command in the terminal window of the running script.

The log gets hidden away. If you need to check it, you must open the server_log.txt files.

If you followed this guide here, you should have everything correctly in place for the script to work.

Remember to change your_cluster_name to the folder name of your cluster in ~/.klei/DoNotStarveTogether.

For this script to work you need screen installed on your system, but chances are you already have it.

#!/bin/bash

#### User Variables; Set your preferences ####

CLUSTER_NAME="your_cluster_name"
MASTER_NAME="Master"
CAVE_NAME="Caves"

CHECK_UPDATE_FREQ="20m"

DONT_STARVE_CLUSTER_DIR="$HOME/.klei/DoNotStarveTogether"
DONT_STARVE_DIR="$HOME/dontstarvetogether_dedicated_server"

#### Script Variables; Do not modify ####

lv_file="/tmp/${CLUSTER_NAME}_latest_version"
DONT_STARVE_BIN="./dontstarve_dedicated_server_nullrenderer"

#### Functions ####

function needs_update() {
    klei_url="https://forums.kleientertainment.com/game-updates/dst/"
    latest_version=$(curl -s $klei_url | grep -Po "\s+\d{6,}$" | head -n 1)
    if [[ -e $lv_file ]]; then
        current_version=$(head -n 1 $lv_file)
    fi
    echo $latest_version > $lv_file
    if [[ $current_version -lt $latest_version ]]; then
        echo "1"
    else
        echo "-1"
    fi
}

function exists() {
    if [[ ! -e $1 ]]; then
        failed "File/Dir not found: $1"
    fi
}

function failed() {
    echo "Error: $@" >&2
    exit 1
}

function command_m() {
    screen -S ${CLUSTER_NAME}_${MASTER_NAME} -p 0 -X stuff "$1^M"
}

function command_c() {
    screen -S ${CLUSTER_NAME}_${CAVE_NAME} -p 0 -X stuff "$1^M"
}

function stop_server() {
    command_m 'c_announce("Server stopped by force! Shutting down!")'
    sleep "2s"
    command_c 'c_shutdown()'
    command_m 'c_shutdown()'

    # The server gets 20 seconds to shutdown normally #
    sleep "20s"

    # Should the server still be running; Kill it #
    if screen -list | grep -q ${CLUSTER_NAME}; then
        command_c "^C"
        command_m "^C"
    fi
    exit 1
}

#### Script routine ####

trap stop_server 2

# Check for missing files #

exists "$DONT_STARVE_CLUSTER_DIR/$CLUSTER_NAME/cluster.ini"
exists "$DONT_STARVE_CLUSTER_DIR/$CLUSTER_NAME/cluster_token.txt"
exists "$DONT_STARVE_CLUSTER_DIR/$CLUSTER_NAME/$MASTER_NAME/server.ini"
exists "$DONT_STARVE_CLUSTER_DIR/$CLUSTER_NAME/$CAVE_NAME/server.ini"

# Check for an update beforehand #
needs_update 2>&1 >/dev/null

while [[ true ]]; do

    # Force an update #
    mv "$DONT_STARVE_DIR/mods/dedicated_server_mods_setup.lua" "$DONT_STARVE_DIR/mods/dedicated_server_mods_setup.lua.bak"
    echo "Start updating the game."
    steamcmd +force_install_dir $DONT_STARVE_DIR +login anonymous +app_update 343050 validate +quit
    mv "$DONT_STARVE_DIR/mods/dedicated_server_mods_setup.lua.bak" "$DONT_STARVE_DIR/mods/dedicated_server_mods_setup.lua"

    # Check for DST binary #
    exists "$DONT_STARVE_DIR/bin"

    # Run Shards #
    cd "$DONT_STARVE_DIR/bin"
    echo "Starting ${CAVE_NAME}."
    screen -d -m -S ${CLUSTER_NAME}_${CAVE_NAME} $DONT_STARVE_BIN -cluster $CLUSTER_NAME  -shard $CAVE_NAME
    echo "Starting ${MASTER_NAME}."
    screen -d -m -S ${CLUSTER_NAME}_${MASTER_NAME} $DONT_STARVE_BIN -cluster $CLUSTER_NAME  -shard $MASTER_NAME
    # Checks for updates #
    while [[ true ]]; do
        
        # Check for updates every 20 minutes #
        sleep $CHECK_UPDATE_FREQ

        # If there is an update, we will start the shutdown process #
        result=$(needs_update)
        if [[ $result -gt 0 ]]; then
            echo "The server needs an update. Will restart in 15 minutes."
            command_m 'c_announce("Klei released an update! The server restarts in 15 minutes!")'
            sleep "10m"
            command_m 'c_announce("Klei released an update! The server restarts in 5 minutes!")'
            sleep "4m"
            command_m 'c_announce("Klei released an update! The server restarts in 1 minute!")'
            sleep "1m"
            command_m 'c_announce("Restarting now!")'
            command_c 'c_shutdown()'
            command_m 'c_shutdown()'
            break
        fi
    done

    # We wait till the game shuts down #
    echo "Waiting for shards to shut down."
    while [[ true ]]; do
        if ! screen -list | grep -q ${CLUSTER_NAME}; then
            echo "Shards are down. Restarting."
            break
        fi
    done
done

 

Edited by BakaSchwarz
Found a typo
  • Like 3

Share this post


Link to post
Share on other sites
cezarica    424

You know you can use builds.json (mentioned in here) file to get the version from the 'release' branch, right?

In Perl i get it like this:

sub getLast {
        my $release = 0;
        my $url = 'https://s3.amazonaws.com/dstbuilds/builds.json';
        my $response = HTTP::Tiny->new->get($url);
        if ($response->{success}) {
                $release = decode_json($response->{content})->{'release'}[-1];
        }
        return $release;
}

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now