SRCDS Steam group


Kill Script ?
#1
Does anyone know of a stop server script that can be executed to stop an instance of CS:S ?

I am using Webmin with Fedora Core4 and want to have a custom command to stop a server as well as start it. The start is simple as I can script out a ./server.sh, but stopping is another thing altogether since I dont know what the PID ID would be....

I want to allow people to access startign and stopping the server via webmin since I cant always be around....

Reply
#2
this is going to sound like a shameless plug but here ya go none the less

http://forums.srcds.com/showthread.php?tid=1479

This can be intergrated into webmin fairly easily. If you need assistance with it let me know via email on this forum and i will respond as soon as I get it.
Reply
#3
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: 7)
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)