SRCDS Steam group


Script to reboot cs source servers on a specific time
#1
hello,

i found somewhere on the internet ( had to do a lot of moves to get this file from a site which is not online anymore.. thanks google )a nice script for linux servers which can automatically reboot your cs source server for example during the night.

why need a reboot script ? well from what i have noticed myself, is that the memory for instance is eaten and increases. had my cs source server running for 4 weeks without restarting, which ended up of 67 % of the memory been used by the source engine.

For 1 server it is not such a problem but if you have a lot of game servers running then it would be nice that an automatic process can kick your servers offline and bring it back in order to reset the memory usuage.

So you have a clean fresh server everyday.

This script was written by : Ole Christian

What do you need for this script :

- screen function ( google on your linux version and screen )
- crontab -e

This is the code :

Code:
#!/bin/sh
#############################################
## SRCDS SERVER STARTUP SCRIPT V1.0        ##
## [10.10.2005]                            ##
## Created by:                             ##
## Ole Christian Rynning <oc@rynning.no>   ##
## jce #norge.css @ quakenet               ##
#############################################

#############################################
## CUSTOMIZE THESE VARIABLES TO YOUR NEEDS ##
#############################################

###
# SCRDS_NAME is the name of the instance
# for example customer_css, clanwarserver,
# and so on.
# Default: css
SRCDS_NAME="css"
###
# SCRDS_BIN is the binary used to run the
# server, for srcds linux, it is commonly
# ''srcds_run''
#
# The reason it is prefixed by ./ is that
# the script by default changes dir to the
# SRCDS_PATH directory before running the
# binary
SRCDS_BIN="./srcds_run"
###
# SRCDS_PATH defines the physical location
# where srcds is installed;
# for instance:
# ''/home/customer/srcds/''
SRCDS_PATH="."
###
# SRCDS_PIDFILE lets you specify a pidfile.
# if you want the pidfile in another location
# than the base dir of $SRCDS_PATH, you need
# to specify the full path;
# for instance:
# ''/home/customer/tmp/warserver.pid''
SRCDS_PIDFILE="${SRCDS_NAME}.pid"
###
# SRCDS_OPTS is all the default start options
# for the server that you cannot set in the
# server config files. Options such as tickrate,
# ip, port and max_fps are set here
SRCDS_OPTS="-game cstrike -console \
    -autoupdate -pidfile ${SRDCS_PIDFILE} \
    -tickrate 100 +maxplayers 12 +map de_dust2 \
    -ip 217.78.96.31 -port 27015 +fps_max 600"

#############################################
##  DO NOT EDIT ANYTHING BELOW THIS LINE!  ##
#############################################
usage() {
    echo "Usage: ${SRCDS_NAME} (stop|start|restart)"
    exit 2
}

if [ -z $1 ]; then
    usage
fi

srcds_start() {
    OWD=`pwd`
    cd $SRCDS_PATH
    screen -AmdS ${SRCDS_NAME} ${SRCDS_BIN} ${SRCDS_OPTS}
    cd $OWD
}

srcds_stop() {
    screen -dr ${SRCDS_NAME} -X quit
}

srcds_restart() {
    srcds_stop
    srcds_start
}

    
case $1 in
    stop)
        srcds_stop
    ;;
    start)
        srcds_start
    ;;
    restart)
        srcds_restart
    ;;
    *)
        usage
esac
        
exit 0


The installation is actually really simple :

- Download the file from the attached file on this post.
- Or do this in your console : wget http://www.cs-source.nl/files/srcds.sh

- place the file srcds.sh in the same folder as where your srcds_run is placed
- do a : chmod 777 srcds.sh in console
- edit the srcds.sh

Most important changes are :

- SRCDS_PATH
- SRCDS_OPTS

Make sure you fill in the full path to the srcds.sh file.
For example : SRCDS_PATH="/home/peter/srcds/
If you do not do this then the script will not restart your server.

One comment on editing the file srcds.sh, make sure you do this in an editor which is capable of linux enviroment. If you edit it on your notepad in windows and put it back, you will get some funky errors about missing parameters...

Use nano or vi editor.

Oke now you need to edit the crontab option

- use command : crontab -e

fill in the following command for example :

* 8 * * * /path/to/srcds.sh restart >/dev/null 2>&1

This cronjob will give a command to srcds.sh to restart on 8 o clock.
( check google for more info about cronjobs )

This command '>/dev/null 2>&1' makes sure the restart msg will not be mailed.

Start the script using in the folder of where it standing with :

- ./srcds.sh start

It will now start the server in the background using screen.
Check with screen -r if the server is starting.

That's it actually.

Looks hard but it's quite simple.
Been using it for a couple of days now and it really works well.


Attached Files
.zip   srcds.zip (Size: 1.09 KB / Downloads: 114)
Reply
#2
sounds good ill give it a try right now. Im getting an error on line 56 i cant figure it out.
Reply
#3
sorry, had to change my nick here at work. forgot password....

oke well the problem you encounter is because you have edited the srcds.sh NOT on your linux server........

i had this same situation by using a ftp program and tried to edit the parameters and then upload it back again and received the same error as you stated.

just edit the file on the server itself and it will work.
Reply
#4
this is how i use the script now.( works only if you have root access )

- added a crontab in root
01 06 * * * /sbin/shutdown -r now
now my whole server is rebooting at 06:01 am

- added a crontab in my user account
15 06 * * * /home/name/cssource/srcds.sh start >/dev/null 2>&1
now my source server starts itself up.

so now i have an automated sequence every morning.

every morning my server reboots and then starts my source server again resulting in a clean fresh machine in the morning....

and best of it i do not have to handle this kind of stuff manualy anymore..

Reply
#5
That was infact scribbled together by me! anyway I wrote a newer version now that has update (and restart) and restart (without-update) and is abit easier to understand (and works with rc / init.d).

Its perfect if you run several different games and do not wish to inter-link (symlink or hardlink) content such as maps, etc.. But thats another discussion and mostly applying only if you run big clusters and want optimal hard-drive tweaking of all the warservers and minimal hard-drive use.

I work at primetime.no, as a systems developer, and I think we have the baddest warservers of norway (I've tweaked the routers, servers, etc myself, and I think our peering partners and so on should speak for itself compared to other NOCs), but due to that we're focusing on enterprise development, I only host a couple of private servers for my self. But nonetheless, I'm lazy so I always write stuff to accomodise anything I do more than once. :p

I've also taken out pidfile, as it really isn't needed aslong as its run in a screen and not through supervise (ejb's daemontools) which was my first setup.

I've edited the examples to apply to an aimserver, residing in /fireforge/games/css-aim/[cstrike/*]

Code:
#!/bin/sh
#############################################
## SRCDS SERVER STARTUP SCRIPT V1.0        ##
## [10.10.2005]                            ##
## Created by:                             ##
## Ole Christian Rynning <oc@rynning.no>   ##
## IRC-nick: look for someone with juice   ##
## or jce in the nickname                  ##
## #scandinavian.css @ quakenet            ##
## #norge.css @ quakenet                   ##
## #mtnine @ quakenet                      ##
#############################################

#############################################
## CUSTOMIZE THESE VARIABLES TO YOUR NEEDS ##
#############################################

###
# SCREEN_NAME is the nametag on the screen
# session that will run the application.
# Each server instance should have a separate
# screen name. I usually prefix these css_serverid
# with the serverid being a numeric responding
# to the userid of its admin/owner, but you
# can name it anything you like :)
SRCDS_NAME="css-aim"

###
# SCRDS_BIN is the binary used to run the
# server, for srcds linux, it is commonly
# ''srcds_run''
#
# The reason it is prefixed by ./ is that
# the script by default changes dir to the
# SRCDS_PATH directory before running the
# binary
#SRCDS_BIN="./srcds_run"
# Or for instance to run as user ''gameserver'' (look below)
SRCDS_BIN="sudo -u gameserver ./srcds_run"

###
# SRCDS_PATH defines the physical location
# where srcds is installed;
# for instance:
# ''/home/customer/srcds/''
# ''/fireforge/games/css-aim'' (the homedir of css-aim server)
SRCDS_PATH="/fireforge/games/${SRCDS_NAME}"


SRCDS_PIDFILE="/fireforge/games/css-aim/${SRCDS_NAME}.pid"

# SRCDS_OPTS is all the default start options
# for the server that you cannot set in the
# server config files. Options such as tickrate,
# ip, port and max_fps are set here
###
# SRCDS_OPTS contains all the default start options
# for the server that you cannot set in the
# server config files. Options such as tickrate,
# ip, port and max_fps are set here.
#
# I generally put them all on one line, but you can
# split them into several lines by appending "\" on
# the end of each line.
SRCDS_OPTS="-game cstrike \
    -console \
    -tickrate 100 \
    +maxplayers 12 \
    +map de_dust2 \
    -ip 217.78.96.31 \
    -port 27015 \
    +fps_max 600"

#############################################
##  DO NOT EDIT ANYTHING BELOW THIS LINE!  ##
#############################################
usage() {
    echo "Usage: ${SRCDS_NAME} (stop|start|restart|update)"
    exit 2
}

if [ -z $1 ]; then
    usage
fi

srcds_start() {
    OWD=`pwd`
    cd $SRCDS_PATH
    screen -AmdS ${SRCDS_NAME} ${SRCDS_BIN} ${SRCDS_OPTS}
    cd $OWD
}

srcds_stop() {
    screen -dr ${SRCDS_NAME} -X quit
}

srcds_restart() {
    srcds_stop
    srcds_start
}

srcds_update() {
    srcds_stop
    # Prepend -autoupdate to the rest of the options
    SRCDS_OPTS="-autoupdate $SRCDS_OPTS"
    srcds_start
}

case $1 in
    stop)
        srcds_stop
    ;;
    start)
        srcds_start
    ;;
    restart)
        srcds_restart
    ;;
    update)
        srcds_update
    ;;
    *)
        usage
esac

exit 0

Since I use debian i usually place the scripts in /etc/init.d

When running several servers and rebooting them / updating them you shouldn't do them all on the same time.

Fireforge for instance runs 2 warservers, one aimserver and a warserver. I start rebooting them around 7 am..
Code:
12 07 * * * /etc/init.d/css-war update >/dev/null 2>&1
26 07 * * * /etc/init.d/css-aim update >/dev/null 2>&1


If you want to provide a user interface to restart, you could add some sort of CGI or other scriptlanguage and allow for instance server restarts done thorough the web: (I've assumed a non-sudo SRCDS_BIN here for the example effect)

PHP:
Code:
<?php
  system("/usr/bin/sudo -u gameserver /etc/init.d/css-aim restart", $exit_code);
  ( $exit_code != 0 ) or die("Couldn't restart server);
?>

Ruby (erb syntax):
Code:
<%= "couldn't restart server" unless system("sudo -u gameserver /etc/init.d/css-aim restart") %>

and so on with perl `xxx.sh restart`, etc.

I use sudo for allowing the webserver running as ''www-data'' to run the gameservers as non-privileged user ''gameserver'' by adding to /etc/sudoers (with the ''visudo'' program):
Code:
www-data  ALL = (gameserver) NOPASSWD: /etc/init.d/css-aim restart,

(I acctually have a group of admins who can run these commands, and who cannot, and so on, but thats out of scope for here)

For server startups I tend to like the system defaults. Most Linux distros have either a /etc/init.d or a /etc/rc.* structure for this.

I use Debian, so I simply do a:
Code:
update-rc.d css-aim defaults

To make it start on reboots, init level changes and such
Reply
#6
Use ace reboot
My Scripts:

SSHOP -> Buy things in one shop
SDM -> DeathMatch
SZM -> Zombie Mod
SWS -> Weapon Sell
SB -> Beams Mods
STK -> TK Server Management

Subscribe My Scripts And When Out A New Version You Are Informed

[Image: EventScriptsScripter.png]
Reply
#7
How can I execute that script with an php interface, to start stop or restart the server?
Reply
#8
with this you can start,stop and restart
http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
Reply
#9
element Wrote:How can I execute that script with an php interface, to start stop or restart the server?

This is already shown. Just follow the instructions at the top of the file and then:

To restart:

<?php
system("/usr/bin/sudo -u gameserver /etc/init.d/css-aim restart", $exit_code);
( $exit_code != 0 ) or die("Couldn't restart server);
?>

To start:

<?php
system("/usr/bin/sudo -u gameserver /etc/init.d/css-aim start", $exit_code);
( $exit_code != 0 ) or die("Couldn't start server);
?>

To stop:

<?php
system("/usr/bin/sudo -u gameserver /etc/init.d/css-aim stop", $exit_code);
( $exit_code != 0 ) or die("Couldn't stop server);
?>

Install a HTTP daemon like apache, then install the PHP modules. Then just create a simple HTML form with a command filed (and password field maybe, just for security). Set the target as your PHP script, and catch
the POST variables in your PHP script. From that, execute one of the above commands. Hey presto, a (albeit incredibly simple) start/stop/restart Web interface Smile

My advice would be to try and find a simple PHP tutorial. Don't worry, PHP is very simple!
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)