Counterstrike Source Scripts

With these scripts I maintain a CSS server.

My server runs on FreeBSD. Since the CSS software is not available as source, I decided to run it within a jail. It runs under linux-compat kernel module.

There is a simple script in /jail/usr/local/etc/rc.d/gameservers just calls the startupscript:

#!/bin/sh
USER=CHANGETHIS
script=/home/$USER/bin/css
sucmd="su - $USER"

case $1 in
  start)
        $sucmd $script tsstart
        $sucmd $script start
        ;;
  stop)
        $sucmd $script tsstop
        $sucmd $script stop
        ;;
esac 

(Change the variable USER to your jail user under which the CSS will run)

The /jail/home/$USER/bin/css script looks like this:

#!/bin/sh

# start and maintain the counter strike source server
# copyright (c) 2006 tom@linden.at
#

USER=CHANGETHIS
screenname="CounterStrike"
screenbase="screen -S $screenname"
screenstart="-A -m -d"
srcdir="/home/$USER/css"
srcbin="srcds_run"
srcpid="$srcdir/src.pid"
slots=20
#srcplugs="-debug"
srcopts="-game cstrike -pidfile $srcpid $srcplugs +map de_dust +maxplayers $slots -autoupdate"
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
myself="Counter Strike Source Server"
uid="1003"
ts="Team Speak 2.0.20.1 Linux Server"
tsbase="/home/$USER/teamspeak"
tsbin="teamspeak2-server_startscript"


runas=`id -u`
if test "$runas" != "$uid"; then
  echo "$0 must be executed under uid $uid ($USER)!"
  exit 1
fi


ts_start() {
  cd $tsbase
  ./$tsbin start
}

ts_stop() {
  cd $tsbase
  ./$tsbin stop
}

ts_restart() {
  cd $tsbase
  ./$tsbin restart
}

ts_status() {
  echo -n "   TS Process: "
  for pid in `
  ps -U $USER | grep server_linux | grep -v grep | awk '{print $1}'
  `; do
    echo -n "$pid ";
  done
  echo
}

css_start() {
  if test -f "$srcpid"; then
    pid=`cat $srcpid`
    if ps axu | grep $pid | grep -v grep > /dev/null 2>&1; then
      echo "$myself is already running [$pid]"
      exit 1
    else
      echo "$myself crashed. Forcing restart"
      rm -f $srcpid
    fi
  fi
  echo -n "$myself ..."
  cd $srcdir && $screenbase $screenstart ./$srcbin $srcopts
  echo " started."
}


css_restart() {
  if test -f "$srcpid"; then
    pid=`cat $srcpid`
    if ps axu | grep $pid | grep -v grep > /dev/null 2>&1; then
      echo -n "$myself ..."
      kill $pid
      echo " restarted."
    else
      echo "$myself is not running!"
      rm -f $srcpid
      exit 1
    fi
  else
    echo "$myself is not running - no pidfile found!"
  fi
}

css_stop() {
  pid=`ps -U $USER | grep srcds_run | grep -v $screenname | grep -v grep | awk '{print $1}'`
  if test -n "$pid"; then
    kill $pid
    echo "CSS Stopped"
  else
    echo "CSS not running"
  fi
}

css_status() {
  echo -n "Screensession: "
  screen -list |grep $screenname
  echo -n "  CSS Process: "
  ps -U $USER | grep $srcbin | grep -v grep | awk '{print $1, $2, $3, $4}'
}

css_console() {
  screen -S $screenname -r
}

css_paint() {
  clear
  echo
  echo "   Counter Strike Source Server ck.daemon.de"
  echo
  echo "----------------------[ Status ]----------------------"
  css_status
  ts_status
  echo "----------------------[ Status ]----------------------"
  echo
  echo "   1 Counterstrike Server Console"
  echo "   2 Counterstrike Server Stoppen"
  echo "   3 Counterstrike Server Starten"
  echo " * 4 Counterstrike Server Neu Starten (restart)"
  echo "   5 Teamspeak Server Stoppen"
  echo "   6 Teamspeak Server Starten"
  echo " * 7 Teamspeak Server Neu Starten (restart)"
  echo "   8 Bash Command Shell"
  echo "   q Ende"
  echo
}


css_menu() {
  while :; do
    css_paint
    echo -n "> "
    read cmd
    if test -n "$cmd"; then
      case $cmd in
        1) css_console;;
        2) css_stop;;
        3) css_start;;
        4) css_restart;;
        5) ts_stop;;
        6) ts_start;;
        7) ts_restart;;
        8) bash;;
        q) exit;;
        *) echo "invalid input!";;
      esac
      echo -n "press any key to continue> "
      read enter
    fi
  done
}

if test -n "$1"; then
   case $1 in
        start) css_start;;
        stop)  css_stop;;
        restart) css_restart;;
        status) css_status;;
        tsstart) ts_start;;
        tsstop) ts_stop;;
        tsrestart) ts_restart;;
        tsstatus) ts_status;;
        *) echo "usage: $0 { start   | stop   | restart   | status   }"
           echo "       $0 { tsstart | tsstop | tsrestart | tsstatus }"
           ;;
   esac
else
   css_menu
fi 

This script allows to start/stop/restart the TS and CSS servers. It also provides an simple interactive menu for these jobs. Please note, the CSS server will run inside a screen session, because this crappy piece of shit doesn't detach from terminal after being started. I wonder why it's called a "server". However, you can connect anytime to this screen session and enter commands or something.

A last note: be prepared that in case you activated the autoupdate feature of CSS (which you should), you shall have a copy of the gameinfo.txt file, because the autoupdater overwrites it.

Finally: the whole shit is not supported. Use for your own risk. Dont ask me questions about using it on linux, other environments or the like. Instead, try to learn a little bit about unix!