SRCDS Steam group


Linux init script to start server on boot
#31
Here is my sturtup script. It is more simple and without using pid files.

Writen for use with OpenSuSE 11.4 x32_64, but will work with any other GNU/Linux system
Dependencies for clean minimal server (package names from OpenSuSe repos):
glibc-32bit
libstdc++45-32bit
gdb
screen
gzip2

Code:
#!/bin/bash

### BEGIN INIT INFO
# Provides:       css1
# Required-Start: $network
# Required-Stop:  $network
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Valve dedicated server
# Description:    Counter-Strike Source Server
### END INIT INFO

SERVERNAME="VIPGAMES CS:S Server #1"
USERNAME="css"
SERVICE="css1"
BINDIR="/home/css/css1/orangebox"
BIN="srcds_run"
IP="10.0.12.7"
PORT="27015"
MAP="de_dust2"
GAME="cstrike"
MAXPLAYERS="22"
OPTS="./$BIN -game $GAME -ip $IP -port $PORT -insecure +sv_lan 0 +maxplayers $MAXPLAYERS +map $MAP"


CURRENT_USER=`/usr/bin/whoami`

as_user() {
    if [ $CURRENT_USER == $USERNAME ] ; then
        bash -c "$1"
    else
        su - $USERNAME -c "$1"
    fi
}

case "$1" in
    start)
        if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
        then
            echo "$SERVERNAME is already running!"
        else
            echo "STARTING $SERVERNAME at $IP:$PORT ... "
            as_user "cd $BINDIR && screen -dmS $SERVICE $OPTS"
            sleep 1
            if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
            then
                echo "OK"
            else
                echo "ERROR"
            fi
        fi
        ;;
    stop)
        if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
        then
            echo "SHUTING DOWN $SERVERNAME at $IP:$PORT ... "
            kill -9 `ps axw | grep -v grep | grep -v -i SCREEN | grep ${BIN} | grep ${IP} | grep $PORT | awk '{print $1}'`
            sleep 1
            if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
            then
                echo "ERROR - $SERVERNAME is still runing at $IP:$PORT"
            else
                echo "OK"
            fi
        else
            echo "$SERVERNAME is not runing"
        fi
        ;;
    restart)
        if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
        then
            echo "RESTARTING $SERVERNAME at $IP:$PORT ... "
            kill -9 `ps axw | grep -v grep | grep -v -i SCREEN | grep ${BIN} | grep ${IP} | grep $PORT | awk '{print $1}'`
            sleep 1
            if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
            then
                echo "ERROR - $SERVERNAME is still runing at $IP:$PORT, couldn't restart it"
            else
                as_user "cd $BINDIR && screen -dmS $SERVICE $OPTS"
                echo "OK"
            fi
        else
            echo "$SERVERNAME is not runing"
        fi
        ;;
    status)
        if ps axw | grep -v grep | grep -v -i SCREEN | grep $BIN | grep $IP | grep $PORT > /dev/null
        then
            echo "$SERVERNAME is running."
        else
            echo "$SERVERNAME is not running."
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/$SERVICE {start|stop|restart|status}"
        exit 1
        ;;
esac
Reply
#32
I have made a small modification to the script to solve an issue I was having. If I force-restarted the machine and the script never got to remove the screen-pid-file, it would fail to start again. I added the following lines right below the service_start()-line:

Code:
# If screen pid file exists but not the tf2 one
if [ -f $STEAM/$GAME/$NAME-screen.pid ] && [ ! -f $STEAM/$GAME/$NAME.pid ]; then
    # Check if autoupdate is running. If it isn't, we know we can safely delete the pid file
    if ! pgrep 'steam'; then
        echo "Auto-update isn't running, so nothing stops us from deleting the pid file!"
        rm $STEAM/$GAME/$NAME-screen.pid
    fi
fi

This isn't fail-safe, but it should do the trick. Hope it helps someone!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)