SRCDS Steam group


Restart server periodically?
#1
I was wondering if there is a way to restart a server at a given time everyday (4am for example).

Could it be done using a cron job (something which I know nothing about)?

I've seen plugins (there's a script for Eventscripts I believe) that supports this kind of thing, but I'd rather not bloat my server just for this small purpose.

Thanks.
Reply
#2
Say you have a script to start your server named go.sh:

Code:
#!/bin/sh gamedir="/home/<username>/srcds/" cd $gamedir process=`ps auxxwww | grep ./srcds_run | grep -v grep | awk '{print $6}'` if [ -z "$process" ]; then echo "Couldn't Find Game Running. Restarting" nohup ./server.sh echo"ok" fi

Which checks for the srcds_run process and if it can't find it, it will start the server using another batch called server.sh:

Code:
screen -A -m -d -S sskcs ./srcds_run -game cstrike -ip blah -port 27015 -maxplayers 18 +map de_dust2 -tickrate 100 -autoupdate

You would run go.sh every minute using cron task (terminal: crontab -e):

Code:
* * * * * /home/<username>/srcds/go.sh >/dev/null 2>&1

Now, to restart the server at 4am, all you need to do is execute a separate batch to kill the srcds process and the go.sh batch will kick in to restart the server. In this case, its called kill.sh:

Code:
killall -9 screen killall -9 srcds_run

And you would run this at 4am every day using:

Code:
0 4 * * * /home/<username>/srcds/kill.sh >/dev/null 2>&1
Reply
#3
Wow. Thanks for the detailed answer. I use linux all the time at university but unfortunately have never used cron.

Muppet Wrote:You would run go.sh every minute using cron task (terminal: crontab -e):

Code:
* * * * * /home/<username>/srcds/go.sh >/dev/null 2>&1

I know this would be a tiny process to run, but would it be better to just run it between 03:50 and 4:15 or something (so that it starts the server after a kill)? And if so, how?

Muppet Wrote:Now, to restart the server at 4am, all you need to do is execute a separate batch to kill the srcds process and the go.sh batch will kick in to restart the server. In this case, its called kill.sh:

Code:
killall -9 screen killall -9 srcds_run

And you would run this at 4am every day using:

Code:
0 4 * * * /home/<username>/srcds/kill.sh >/dev/null 2>&1

Will the kill commands only effect my user account? They won't touch other users' screens/srcds_runs?

Also, I've never used crontab before. Do I just enter these commands on seperate lines?

So, run crontab -e, then enter:

Code:
* * * * * /home/<username>/srcds/go.sh >/dev/null 2>&1 0 4 * * * /home/<username>/srcds/kill.sh >/dev/null 2>&1

Many thanks again for your help.
Reply
#4
go.sh is best run every minute. Its there just to make sure the server is always up and running. If it detects the server is not running at all (for any reason - random crash say at 10pm), it will detect it within 1 minute and restart the server for maximum uptime.

Yes, those cron tasks will only be run under your user account as long as you dont 'sudo crontab -e' and just 'crontab -e' (cron tasks for your user only).

And yes, exactly. One per line.
Reply
#5
Excellent.

Just one quick check - 0 4 * * * wont be run at every minute inside 4 o'clock will it? My head is telling me it should be 0 4 0 0 0 (or something similar), but I'm sure I'm wrong.

Thanks again.
Reply
#6
Nah, that would be * 4.

minute hour day month dayofweek

where * = do it every
Reply
#7
Ah ok. Thanks Smile
Reply
#8
I use these two scripts - and have cron run the restart version of them every day at 5am. I utilize webmin in order to input all of my cronjobs. : )

Startscrtiptcss1
Code:
#!/bin/sh ##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 cd /srcds_l . ./startscriptcss1.conf service_start() { #This is the startup script, which will start the server based off of the .conf file specified above, and write the screen name as the pid if [ ! -f $DSPATH/$DSNAMEdonotuse.pid ] && [ ! -f $DSPATH/$DSNAMESHORT.pid ]; then if [ -x $DSPATH/$DSNAMEdonotuse ]; 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 }' > $DSNAMESHORT.pid echo -e "$DSNAMESHORT Process ID Written to $DSNAMESHORT.pid\n$DSNAMESHORT Server Process ID Written to $DSNAMESHORT.pid" cp $DSNAMESHORT.pid /srcds_l/cstrike/$DSNAMESHORT.pid fi else echo -e "Server is Already running." 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/$DSNAMESHORT.pid); do kill $vdspid; rm -rf $DSNAMESHORT.pid; break; done rm -rf $DSPATH/cstrike/$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 $DSNAMESHORT.pid file exsists if [ -f $DSPATH/$DSNAME$DSNAMESHORT.pid ]; then # Pulling in the values to evaulate if they are true or not PROCESSRUN=`cat $DSPATH/$DSNAME$DSNAMESHORT.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 "$DSNAMESHORT is running on process `cat $DSPATH/$DSNAMESHORT.pid`\n" # It found this process ID matches and is outputting what it is currently running on fi else echo -e "$DSNAMESHORT 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/$DSNAMESHORT.pid ]; then echo "$DSNAMESHORT Server is running on process `cat $DSPATH/cstrike/$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" else # Looks like noone is watching it right now.... 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/cstrike/*.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

startscriptcss1.conf
Code:
#!/bin/sh CLIENT='css1'; # Identifier for attaching screen session TITLE='SRCDS'; # Name that is shown during start-up messages DSPATH='/srcds_l'; # Where Steam is installed DSIP='24.23.21.20'; # IP address you want to use to start server with DSNAME='srcds_run'; # Either hlds_run or srcds_run DSNAMESHORT='css1'; # Label only DSGAME='cstrike'; # Game type (valve, cstrike, czero, etc) DSIMAP='de_dust'; # Load this map initially DSPORT='27015'; # Game server listens on this UDP port DSSIZE='32'; # Maximum number of players to allow DSUSER='gaming'; # Which user is running the process DSTICKRATE='66'; # Tick Rate DSSERVERFPS='600'; # Server FPS Rate DSCFGFILE='server1.cfg'; # Choice of which server config to draw from # Don't edit this unless you need the script to do something special DSOPTS="-game $DSGAME +map $DSIMAP -ip $DSIP -port $DSPORT -rcon_port $DSPORT -autoupdate +maxplayers $DSSIZE -pidfile $DSNAMESHORT.pid -tickrate $DSTICKRATE +fps_max $DSSERVERFPS +exec $DSCFGFILE" # 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"

I use the ./srcds_l/startscriptcss1 restart in order to restart the server through the crontab.
Reply
#9
Blimey. Thanks for the answer.

I think I'll stick with Muppet's simpler scripts as I tried them last night and they worked perfectly.

Thanks again though.
Reply
#10
Hello there, I could not get this to work for me, not sure if I screwed it up or not here is what I got.

my go.sh
Code:
#!/bin/sh gamedir="/home/<srcds>/srcds/dust2/" cd $gamedir process=`ps auxxwww | grep ./srcds_run | grep -v grep | awk '{print $6}'` if [ -z "$process" ]; then echo "Couldn't Find Game Running. Restarting" nohup ./server.sh echo"ok" fi

Then I have my server.sh

Code:
* * * * * /home/<srcds>/srcds/dust2/go.sh >/dev/null 2>&1

Do I put the < > in my username?
Also when I crontab -e Do I do that as my user or Root. Thanks.
Reply
#11
Yes, you have to replace <srcds> with your username:

Code:
/home/girlyboy/srcds...

crontab -e can be done with just a normal user account - there is no need to do this as root.
Reply
#12
Thanks, found out I never used ctrl x Smile
Woot
Reply
#13
Does anybody know of a script that does exactly this but remotely?

I run a linux webserver with a crontab running on it. I want to run a script that will just issue the quit command via RCON to my gameservers - which are running on a different machine (windows Server 2008 R2). I'd imagine the different OS system is not an issue due to

I have a utility which will restart the server if it detects it to not be running, so a quit will serve my purposes well.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)