SRCDS Steam group


Tutorial: Init.d Script for SRCDS
#1
Just polished up the script I am using for SRCDS, which has been built on top of some of the ones I found on the net.

I use /etc/init.d/SRCDS but you can call it whatever you please.

Using screen to keep the process output detached in the background and using your "newuser" to avoid using root.

Code:
# replace <newuser> with the user you want to run the server as (NOT ROOT)
SRCDS_USER="<newuser>"
# Do not change this path
PATH=/bin:/usr/bin:/sbin:/usr/sbin
# The path to the game you want to host. Replace <filepath> with your path, example = /home/newuser/tf2/orangebox
DIR=<filepath>
DAEMON=$DIR/srcds_run
# Change all PARAMS to your needs.
PARAMS="-console -game tf +map ctf_2fort -maxplayers 30 -autoupdate"
# Change <name> to what you want the detached screen to be called. e.g. SRCDS-TF2
NAME=<name>
DESC="Source Dedicated Server"
case "$1" in
start)
echo "Starting $DESC: $NAME"
if [ -e $DIR ];
then
  cd $DIR
su $SRCDS_USER -l -c "screen -d -m -S $NAME $DAEMON $PARAMS"
else echo "No such directory: $DIR!"
fi
;;
stop)
if su $SRCDS_USER -l -c "screen -ls |grep $NAME"
then
     echo -n "Stopping $DESC: $NAME"
     su $SRCDS_USER -l -c "screen -S $NAME  -X quit "
    echo " ... done."
else
    echo "Couldn't find a running $DESC"
fi
;;
restart)
if su $SRCDS_USER -l -c "screen -ls |grep $NAME"
then
     echo -n "Stopping $DESC: $NAME"
     su $SRCDS_USER -l -c "screen -S $NAME  -X quit "
     echo " ... done."
else
     echo "Couldn't find a running $DESC"
fi
echo -n "Starting $DESC: $NAME"
cd $DIR
su $SRCDS_USER -l -c "screen -d -m -S $NAME $DAEMON $PARAMS"
echo " ... done."
;;
status)
# Check whether there's a "srcds" process
ps aux | grep -v grep | grep srcds_r > /dev/null
CHECK=$?
[ $CHECK -eq 0 ] && echo "SRCDS is UP" || echo "SRCDS is DOWN"
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0


On Ubuntu, add this script to your /etc/init.d directory. <script> = Scriptname e.g. SRCDS-TF2

Code:
sudo update-rc.d <script> defaults

To start the server:
Code:
/etc/init.d/<script> start

To stop the server:
Code:
/etc/init.d/<script> stop

To restart:
Code:
/etc/init.d/<script> restart

To check status:
Code:
/etc/init.d/<script> status
Which will reply either UP (online) ir DOWN (offline.)


In Ubuntu you can also use:
Code:
service <script> restart

and the other start stop status commands.






I do however take no responsibility for any screw ups/ complaints about the hacky nature of the closing of the server.

Built upon the work here:
http://www.freenerd.net/index.php?title=Linux_SRCDS_server
and
http://stevenbenner.com/2010/11/how-to-set-up-a-team-fortress-2-dedicated-server-on-ubuntu/


EDIT: Apologies if this is meant to be in the tutorials section. I made a copy in tutorials, so delete this thread please (it won't let me.)
Reply
#2
How does this differ from:

./hl.sh
Code:
#!/bin/bash
echo "Starting up TF2 Highlander server."
sleep 1
screen -A -m -d -S tf2highlander ./srcds_run -console -game tf -port 27016 +ip 0.0.0.0 +maxplayers 24 +randommap +sv_pure 2 +exec highlander.cfg -autoupdate
echo "Started. Type screen -x to resume!"

?
Reply
#3
(06-28-2012, 07:29 AM)Blah Wrote:  How does this differ from:

./hl.sh
Code:
#!/bin/bash
echo "Starting up TF2 Highlander server."
sleep 1
screen -A -m -d -S tf2highlander ./srcds_run -console -game tf -port 27016 +ip 0.0.0.0 +maxplayers 24 +randommap +sv_pure 2 +exec highlander.cfg -autoupdate
echo "Started. Type screen -x to resume!"

?

You have used a script which is just fine, but depending on which user you run it as it could be undesirable. i.e. don't run it as root. Equally, you have to be in your server's directory in order to execute it and start the server.

For my server, the server automatically starts the server at boot using the "source" user. I can execute the service commands from anywhere and they will start/stop/restart the server.

My script above is saved into /etc/init.d/ on your server and would run like as a service where as yours will be within the server directory.


As a service this (my script) also means you can easily stop, start, restart and check status the service using:

"service servicename start"

"service servicename stop"

"service servicename restart"

"service servicename status"

via command line from anywhere so long as you have access to services.



For me the service name is SRCDS-TF2.

You script opens the server into a new screen like mine, however you would need to go to that screen (screen -x) to execute commands like quit to stop, or other server commands.

Using mine in init.d, running the "services SRCDS-TF2 stop" via terminal would terminate the server from any screen/terminal, rather than moving to the server screen and terminating it otherwise.



Likewise, I use webmin which allows me to map custom commands to buttons inside the website (webmin is a web admin panel for Linux)

It also allows me to give basic access to other users via webmin to my TF2 and teamspeak 3 servers without having to give them access to the server, or server user accounts.

See here: http://i.imgur.com/R2fOI.png

tl;dr

Allows you to add the server as a service and start automatically.
Allows you to start and stop the server with one command rather than having to enter the detached screen.
If you are using Webmin, you can add these commands to a custom panel for non administrative users that you don't want to give access to user accounts.

Hope this helps. I will edit the above to make it more clear to use!
Check out http://resshef.co.cc for more stuff.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)