10-22-2005, 07:19 PM
(This post was last modified: 10-22-2005, 07:20 PM by bloodwraith.)
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.
and the conf file
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
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