Self-updating Server Startup Script - Linux

Recommended Posts

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.


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




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


#### Functions ####

function needs_update() {
    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)
    echo $latest_version > $lv_file
    if [[ $current_version -lt $latest_version ]]; then
        echo "1"
        echo "-1"

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

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"
    exit 1

#### Script routine ####

trap stop_server 2

# Check for missing files #

exists "$DONT_STARVE_CLUSTER_DIR/$CLUSTER_NAME/cluster_token.txt"

# 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}."
    # 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 #
        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()'

    # 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."


Link to comment
Share on other sites

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 = '';
        my $response = HTTP::Tiny->new->get($url);
        if ($response->{success}) {
                $release = decode_json($response->{content})->{'release'}[-1];
        return $release;

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.