SRCDS Steam group


Linux Start script
#1
I figured since i posted it once before on the main steam forums that I would repost it here. I have created a script to help manage the day to day tasks of dealing with most any hlds/srcds server. Most of the bugs have been worked out but any person who wants to use it is welcome to do so. This is brought to you by Jiggly's Fun House and is used currently to control Server 4.

Code:
#!/bin/sh
# Created by BloodWraith @ jiggly.recongamer.com
# This script is currently being managed as a Free (as in beer) script under no specific licensing.
# Start/stop/restart a Valve dedicated server
# All configuration changes should occur in <scriptname>.conf. Only change the name of the call below if you are using a different name for the script
. ./startscript.conf

service_start() {

# Checking to see if root is the current user you are using to start the script
if [ $DSUSER == 'root' ]; then
# Oops is caught you running as root shame on you >:). Well you gotta tell it yes or no.    
    echo -e "You are currently running as $DSUSER, This is dangerous and should be avoided\nAre you sure you want to do this? (yes or no please):"
    read
    case "$REPLY" in
    yes)
# Looks like you chose to run it as root... The script hates you for it but hey do whatever you feel hehe
        echo -e "Alright proceeding running program as $DSUSER"

# Currently checking to see if the pid files that is creates during the startup process exsist. If neither exsists it will go ahead.
        if [ ! -f $DSPATH/$DSNAME.pid ] && [ ! -f $DSPATH/dod/$DSNAMESHORT.pid ]; then
            if [ -x $DSPATH/$DSNAME ]; then
                echo "Starting $TITLE: Services"
                cd $DSPATH; $DSINTERFACE $DSPATH/$DSNAME $DSOPTS
                sleep 1 # prevent race condition on SMP kernels
                ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }' > $DSNAME.pid # Finding and writing current process id of the screen process
                echo -e "$DSNAMESHORT Process ID Written to $DSNAME.pid\n$DSGAME Server Process ID Written to $DSNAMESHORT.pid"
            fi
        else
            echo -e "Server is Already running you sure you want to start this up?" # It found that one of the pid files exsists perhaps the server did not shut down with the script being used... if this is the case use <scriptname> clean to fix it
        fi
    ;;
    no)
# Good choice you did not choose to run as root
        echo -e "Wise choice please login as a different user or run this with su - <username> -c 'command'. "
    ;;
    *)
# What in the heck is wrong with you didn't I say YES or NO
        echo -e "I don't recognize $REPLY as a valid yes or no answer. Please user only yes or no only"
    ;;
    esac
# The script found you were not running as root so it is running like normal. Uses same calls to activate script like above just no input is require to start it this time. You should not be prompted for yes or no if yo uare not root running it this time
else
        if [ ! -f $DSPATH/$DSNAME.pid ] && [ ! -f $DSPATH/$DSNAMESHORT.pid ]; then
            if [ -x $DSPATH/$DSNAME ]; then
                echo "Starting $TITLE: Services"
                echo "$DSPATH/$DSNAME $DSOPTS"
                cd $DSPATH; $DSINTERFACE $DSPATH/$DSNAME $DSOPTS
                sleep 1 # prevent race condition on SMP kernels
                ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }' > $DSNAME.pid
                echo -e "$DSNAME Process ID Written to $DSNAME.pid\n$DSNAMESHORT Server Process ID Written to $DSNAMESHORT.pid"
            fi
        else
            echo -e "Server is Already running you sure you want to start this up?"
        fi
fi
    
}

service_stop() {

# This script is just getting the process id of the server that was written to the file it created earlier so it can kill it.  
    for vdspid in $(cat $DSPATH/$DSNAME.pid);
      do
        kill $vdspid;
    rm -rf $DSNAME.pid;
        break;
    done
    rm -rf $DSPATH/dod/$DSNAMESHORT.pid;
# This command is just clearing out any *DEAD* screen sessions. Those can become a pain really quick
    screen -wipe 1> /dev/null 2> /dev/null
}

service_restart() {

# Simple enough. It is making a call to the stop service and then it waits for a second then it calls the start script

  service_stop
  sleep 1
  service_start
}

service_status() {
# This is checking to see if $DSNAME.pid file exsists
if [ -f $DSPATH/$DSNAME.pid ]; then
# Pulling in the values to evaulate if they are true or not
PROCESSRUN=`cat $DSPATH/$DSNAME.pid`
PROCSERV=`ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }'`

# This is checking to see if the currently running process ID matches with the pid created when the server was started
    if [ "$PROCESSRUN" == "$PROCSERV" ]; then
    echo -e "$DSNAME is running on process `cat $DSPATH/$DSNAME.pid`\n" # It found this process ID matches and is outputting what it is currently running on
    fi
else
    echo -e "$DSNAME is offline. This could be due to someone or something else that killed it." # Apparently the server is offline
            if [ "$PROCSERV" ]; then
            echo -e "However a Process Matching the same criteria was found at process ID $PROCSERV....\n Might be worth a investigation" # Wait, it found another server matching the same criteria running under a different process ID.
            fi
fi

#Checking to see if this file exsists

if [ -f $DSPATH/$DSNAME.pid ]; then
echo "$DSNAMESHORT Server is running on process `cat $DSPATH/dod/$DSNAMESHORT.pid`" # It found the file exsists and is outputting the info
else
echo -e "$DSGAME Server is offline. This could be due to someone or something else that killed it\nor it is just rebooting" #Oops the server is active or the pid file got deleted.
fi
}

# This service is used watch the process that is currently running
service_watch(){

# Check if there is someone already attached

if [ `screen -wipe | grep $CLIENT | grep -v grep | awk '{ print $2 }'` == '(Attached)' ]; then
    echo -e "Someone is already attached to the console of the server.\n Might want to check who" # Oops someone is already attached to it..... better wait your turn or go chew someone $%^ out
else

# Looks like noone is watching it right now.... peeping tom time !!!

    screen -r $CLIENT
fi
}

# This service is used to clean house if the script is reporting erroneous info.

service_clean(){

rm -rf *.pid;
rm -rf $DSPATH/dod/*.pid;
screen -wipe 1> /dev/null 2> /dev/null;
}

case "$1" in
'start')
  service_start
  ;;
'stop')
  service_stop
  ;;
'restart')
  service_restart
  ;;
'status')
  service_status
  ;;
'watch')
  service_watch
  ;;
'clean')
  service_clean
  ;;
*)
  echo "usage $0 start|stop|restart|status|watch|clean"
esac

and the conf file

Code:
#!/bin/sh

CLIENT='test';                # Unique handle this server uses. Hostname
                              # can be reset in your server.cfg

TITLE='Test Script';          # Name that is shown during start-up messages
DSPATH='/steamfolder';    # Where Steam is installed
DSIP='ip';        # IP address you want to use to start server with
DSNAME='srcds_run';           # Either hlds_run or srcds_run
DSNAMESHORT='source';        # Label only
DSGAME='dod';               # Game type (valve, cstrike, czero, etc)
DSIMAP='dod_donner';             # Load this map initially
DSPORT='27015';               # Game server listens on this UDP port
DSSIZE='playersize';                  # Maximum number of players to allow
DSUSER=`whoami`;             # Which user is running the process


# Don't edit this unless you need the script to do something special
DSOPTS="-game $DSGAME +hostname \"$CLIENT\" +map $DSIMAP +ip $DSIP -port $DSPORT -autoupdate +maxplayers $DSSIZE -pidfile $DSNAMESHORT.pid"

# This is the caller for the screen process. Only change if this is different from where your screen process currently resides.
DSINTERFACE="/usr/bin/screen -A -m -d -S $CLIENT"

If you need the files directly just goto jiggly.recongamer.com as I will be posting a download for the code there (as the forums is preventing me from attaching them).

Any improvements or suggestions PLEASE POST THEM Smile
Reply
#2
nifty, ill give it a try, thanks.
Reply
#3
if you have any problems you can contact me on here about it or you can catch me on the site listed in the script.

Also if anyone has any requests I would be more than happy to add them to the script.
Reply
#4
first of all I would like to give much respect to bloodwraith for his script. I am using it right now and with a few modifications it works flawlessly. I simplified it a bit by condensing everything into one file and added a extra variable inorder to get it to work on my Debian 3.1 rc1 linux server with Counter Strike Source and as he stated before im sure it will work with my Call of Duty server that im about to setup! Thanks again bloodwraith! GJ sir!

Heres how I did it..

Quote:#First create a user in debian that you want to run your css server. useradd username

#next log in as that user NOTE: screen will not work if you are su'd #unless you ssh to localhost i.e.
ssh username@localhost

#Create a file called csstart.sh copy and paste the code below into #that file and modify to your liking. OR download the attachment if you dont like editing script files. NOTE: Because this page uses #line wrap the code below will need to be checked for that occurance. #'echo statements and comments all need to be on one line if possible, #etc.
nano csstart.sh

# give the script permissions to be executed
chmod +rx-w ./csstart.sh

#start the server
./csstart.sh
Code:
#!/bin/sh
# Credit for creation of this goes to BloodWraith @ jiggly.recongamer.com I simply modified it a bit for
# debian 3.1 sarge and CounterStrike Source.
# This script is currently being managed as a Free (as in beer) script under no specific licensing.
# Start/stop/restart a Valve dedicated server
# All configuration changes should occur in <scriptname>.conf. Only change the name of the call below if you are using a different name for the script.
### Arcanum remarked this line out and added the conf file to this file for simplicity to me.
### Unremark it and the create the file and add the below info if you want it seperated as  bloodwraith intended
### ./startscript.conf


                CLIENT='csds';                  # This is going to be the name of your SCREEN session.
                                                # Unique handle this server uses. Hostname
                                                # can be reset in your server.cfg if you use one.

                TITLE='CSDS Script';            # Name that is shown during start-up messages
                DSPATH='/home/steam/csds';      # Where Steam is installed
                GDIR='cstrike';                 # Game Directory under steam directory
                DSIP='xxx.xxx.xxx.xxx';            # IP address you want to use to start server with
                DSNAME='srcds_run';             # Either hlds_run or srcds_run
                DSNAMESHORT='source';           # Label only
                DSGAME='cstrike';               # Game type (valve, cstrike, czero, etc)
                DSIMAP='de_nuke';               # Load this map initially
                DSPORT='27015';                 # Game server listens on this UDP port
                DSSIZE='34';                    # Maximum number of players to allow
                DSUSER=`whoami`;                # Which user is running the process




# Don't edit this unless you need the script to do something special
# Don't edit this unless you need the script to do something special
# Don't edit this unless you need the script to do something special

DSOPTS="-game $DSGAME +map $DSIMAP -ip $DSIP -port $DSPORT -autoupdate +maxplayers $DSSIZE -pidfile $DSNAMESHORT.pid -tickrate 66"

# This is the caller for the screen process. Only change if this is different from where your screen process currently resides.
DSINTERFACE="/usr/bin/screen -A -m -d -S $CLIENT"

### this is the end of the ./startscript.conf file that was added to this one.



service_start() {

# Checking to see if root is the current user you are using to start the script
if [ $DSUSER == 'root' ]; then

# Oops is caught you running as root. Are you SURE you want to run as root? YES or NO.
echo -e "You are currently running as $DSUSER, This is dangerous and should be avoided\nAre you sure you want to do this? (yes or no please):"
read
case "$REPLY" in
yes)
# running it as root... This is a huge security no no!
echo -e "Alright proceeding running program as $DSUSER"

# Currently checking to see if the pid files from the startup process exsist. If not then continue normally
if [ ! -f $DSPATH/$DSNAME.pid ] && [ ! -f $DSPATH/$GDIR/$DSNAMESHORT.pid ]; then
if [ -x $DSPATH/$DSNAME ]; then
echo "Starting $TITLE: Services"
cd $DSPATH; $DSINTERFACE $DSPATH/$DSNAME $DSOPTS
sleep 1 # prevent race condition on SMP kernels
ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }' > $DSNAME.pid # Finding and
writing current process id of the screen process
echo -e "$DSNAMESHORT Process ID Written to $DSNAME.pid\n$DSGAME Server Process ID Written to $DSNAMESHORT.pid"
fi
else
echo -e "Found a .pid file, Server is Already running!\nIf your sure the server is not running try (./csstart.sh clean)" # It found that one of the pid files exsists perhaps the server did not shut down with the script being used... if this is the case use <scriptname> clean to fix it
fi
;;
no)
# Good choice... you did not choose to run as root
echo -e "Wise choice please login as a different user or run this with su - <username> -c 'command'.\nNOTE: If you su to another user you will not be able to view a screen session unless you are\ndirectly logged in as that user or you can ssh to localhost!"
;;
*)
# Valid responses are YES or NO
echo -e "I don't recognize $REPLY as a valid yes or no answer. Please use yes or no only"
;;
esac
# The script found you were not running as root so it is running like normal. Uses same calls to activate script like above just no input is require to start it this time. You should not be prompted for yes or no if you are not root running it this time
else
if [ ! -f $DSPATH/$DSNAME.pid ] && [ ! -f $DSPATH/$DSNAMESHORT.pid ]; then
if [ -x $DSPATH/$DSNAME ]; then
echo "Starting $TITLE: Services"
echo "$DSPATH/$DSNAME $DSOPTS"
cd $DSPATH; $DSINTERFACE $DSPATH/$DSNAME $DSOPTS
sleep 1 # prevent race condition on SMP kernels
ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }' > $DSNAME.pid
echo -e "$DSNAME Process ID Written to $DSNAME.pid\n$DSNAMESHORT Server Process ID Written to $DSNAMESHORT.pid"
fi
else
echo -e "Server is Already running!\nIf your sure the server is not running try (./csstart.sh clean)"
fi
fi

}

service_stop() {

# This script is just getting the process id of the server that was written to the file it created
# earlier so it can kill it.
    for vdspid in $(cat $DSPATH/$DSNAME.pid);
      do
        kill $vdspid;
rm -rf $DSPATH/$DSNAME.pid;echo "$DSPATH/$DSNAME $DSNAME.pid has been deleted"
        break;
    done
    rm -rf $DSPATH/$GDIR/$DSNAMESHORT.pid;echo "$DSPATH/$GDIR/$DSNAMESHORT $DSNAMESHORT.pid has been deleted"
# This command is just clearing out any *DEAD* screen sessions. Those can become a pain really quick
    screen -wipe 1> /dev/null 2> /dev/null
}


service_restart() {

# Simple enough. It is making a call to the stop service and then it waits for a second then it calls
# the start script

  service_stop
  sleep 1
  service_start
}

service_status() {
# This is checking to see if $DSNAME.pid file exsists
if [ -f $DSPATH/$DSNAME.pid ]; then
# Pulling in the values to evaulate if they are true or not
PROCESSRUN=`cat $DSPATH/$DSNAME.pid`
PROCSERV=`ps -ef | grep SCREEN | grep "$CLIENT" | grep -v grep | awk '{ print $2 }'`

# This is checking to see if the currently running process ID matches with the pid created when the
# server was started
if [ "$PROCESSRUN" == "$PROCSERV" ]; then
echo -e "$DSNAME is running on process `cat $DSPATH/$DSNAME.pid`\n " # It found this process ID matches and is outputting what it is currently running on
fi
else
echo -e "$DSNAME is offline.\nSomeone or something else may have killed it." # Apparently the server is offline
if [ "$PROCSERV" ]; then
echo -e "However a Process Matching the same criteria was found at process ID $PROCSERV....\n Might be worth a investigation" # Wait, it found another server matching the same criteria running under a different process ID.
fi
fi

#Checking to see if this file exsists

if [ -f $DSPATH/$DSNAME.pid ]; then
echo "$DSNAMESHORT Server is running on process `cat $DSPATH/$GDIR/$DSNAMESHORT.pid`" # It found the file exsists and is outputting the info
else
echo -e "$DSGAME Server is offline.\nSomeone or something else may have killed it or it is just rebooting" #Oops the server is active or the pid file got deleted.
fi
}

# This service is used watch the process that is currently running
service_watch(){

# Check if there is someone already attached

if [ `screen -wipe | grep $CLIENT | grep -v grep | awk '{ print $2 }'` == '(Attached)' ]; then
echo -e "Someone is already attached to the console of the server.\n Might want to check who"
# Oops someone is already attached to it..... better wait your turn or go chew someone $%^ out
else

# Looks like noone is watching it right now.... peeping tom time !!!

screen -r $CLIENT
fi
}

# This service is used to clean house if the script is reporting erroneous info.

service_clean(){

rm -rf *.pid;echo "*.pid has been deleted"
rm -rf $DSPATH/$GDIR/*.pid;echo "$DSPATH/$GDIR/*.pid has been deleted"
screen -wipe 1> /dev/null 2> /dev/null;echo "Screens have been wiped"
}

case "$1" in
'start')
  service_start
  ;;
'stop')
  service_stop
  ;;
'restart')
  service_restart
  ;;
'status')
  service_status
  ;;
'watch')
  service_watch
  ;;
'clean')
  service_clean
  ;;
*)
  echo "usage $0 start|stop|restart|status|watch|clean"
esac

Hope this helps someone!


Attached Files
.txt   csstart.sh.txt (Size: 8.48 KB / Downloads: 52)
Reply
#5
Nice script. I just made a couple of mods for my multiple servers.
ManO-iGO

[Image: 26.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)