Bash - Startscript für Games

#!/bin/bash

## ---------------------------------------------- START SCRIPT OPTIONS
## Fill these with whatever suits your game

# A descriptive name
display_name="Your Fancy Name"

# The reference of your background screen
screen_ref="yourscreenref"

# The directory of your steam installation (where steamcmd lives)
steam_dir="/wherever/your/steam/dir/is"

# The directory, where the game was installed
working_dir="$steam_dir/servers/gamename/for/example"

# This can be changed, if the executable lies in a subdir like bin
binary_path="$working_dir"

# Your own startscript, if you need to define variables or just the executable
binary_name="start_server.sh"

# The process to check for. Usually the executable,
# but sometimes, with wine or mono or the likes,
# it might be better to use the windows executable
check_process="nameoftheprocess"

# The command for patching. Usually ./steamcmd.sh
patch_command="./steamcmd.sh"

# The parameters for the patch command
# substitute with app number from game wiki or website
patch_parameters="+force_install_dir $working_dir +login anonymous +app_update 123456 validate +quit"

# the user for running the process
run_user="games"

## ---------------------------------------------- FUNCTIONS

function sv_getpid
    {
    pid=$(pgrep -f -n $check_process)
    echo $pid
    }

function sv_status
    {
    if [ $(sv_getpid) ];then
        echo "$display_name server is running."
    else
        echo "No $display_name server running (No Process with $check_process present)."
    fi
    }

function sv_preflight
    {
    if [ $(sv_getpid) ]; then
        echo "Process is already running. Stop it first."
        exit 1
    fi
    echo "Starting the $display_name server... "
    
    }

function sv_start
    {
    if [ -e "$binary_path/$binary_name" ]; then
        sudo -u $run_user sh -c "cd $working_dir; screen -dmS $screen_ref ./$binary_name"
            echo "Delay Check..."
        sleep 5
        if [ $(sv_getpid) ]; then
            echo "Done!"
        else
            echo "Failed!"
        fi
    else
        echo "Could not find binary, aborting!"
        exit 5
    fi
    }

function sv_start_view
    {
    
    if [ -e "$binary_path/$binary_name" ]; then
        sudo -u $run_user sh -c "cd $working_dir; screen -dmS $screen_ref ./$binary_name"
    else
        echo "Could not find binary, aborting!"
        exit 5
    fi
    chmod -R 0777 /dev/pts/
    sudo -u $run_user screen -x $screen_ref
    }

function sv_stop
    {
    if [ $(sv_getpid) ]; then
        echo "$display_name server is running"
        echo "Sending $display_name server SIGINT... "
        kill -s 2 $(sv_getpid)
        echo "Done."
    else
        echo "No $display_name server running (No Process with $check_process present)"
        
    fi
    }

function sv_waitforstop
    {
    echo "Waiting for termination..."
    while kill -0 $(sv_getpid) >/dev/null 2>&1 ; do sleep 1 ; done
    }

function sv_patch
    {
    echo "Entering Steam directory..."
    pushd $steam_dir > /dev/null
    echo "Patching..."
    sudo -u $run_user $patch_command $patch_parameters
    echo "Leaving directory..."
    popd > /dev/null
    echo "Done."
    }

## ---------------------------------------------- START PARAMETERS

case "$1" in
    start)
        sv_preflight
        sv_start
    ;;

    startview)
        sv_preflight
        sv_start_view
    ;;

    stop)
        sv_stop
        sv_waitforstop
    ;;

    restart)
        sv_stop
        sv_waitforstop
        sv_start
    ;;

    status)
        sv_status
    ;;

    view)
        chmod -R 0777 /dev/pts/
        sudo -u $run_user screen -x $screen_ref
    ;;

    patch)
        if [ $(sv_getpid) ]; then
            echo "Server is still running. Stop it first or run with patchrestart instead."
        else
            sv_patch
        fi
    ;;

    patchrestart)
        sv_stop
        sv_waitforstop
        sv_patch
        sv_start
    ;;

    *)
        echo "Usage: ${0} {start|startview|stop|restart|status|view|patch|patchrestart}"
        exit 2
esac

exit 0