SRCDS Steam group


Linux init script to start server on boot
#1
Hi everyone,

I'm new to the SRCDS scene, and I have just set up my first dedicated TF2 server on linux. This website has been a great source of information for me setting everything up, so I wanted to give a little back.

I found just a few init scripts around the internet, though nothing really did what I wanted it to do, so I made my own. I like my init scripts to tell me more information that a simple "Service started" message. I also wanted the ability to stop and even restart a service without much effort in case the server failed.

This script is rather simple to configure and should only take you 10 minutes to set up. Configure the script's variables at the beginning to match your server's settings. It's well commented to help you understand what everything does.

Code:
#!/bin/sh
# Source Dedicated Server Init Script

# Server options
TITLE='Source Dedicated Server' # Script initialization title
LONGNAME='Team Fortress 2'        # Full title of game type
NAME='tf2'                          # Server handle for the screen session
DAEMON='srcds_run'                # The server daemon
STEAM='/home/tf2/orangebox'        # STEAM to Steam installation
USER='tf2'

# Game options
IP='72.52.248.250'                # IP of the server
PORT='27015'                    # Port number to
MAP='ctf_2fort'                    # Initial map to start
GAME='tf'                        # Game type (tf|cstrike|valve|hl2mp)
SIZE='24'                        # Maximum number of players

# Server options string
OPTS="-game $GAME +hostname \"$CLIENT\" +map $MAP +ip $IP -port $PORT \
    -autoupdate +maxplayers $SIZE -pidfile $STEAM/$GAME/$NAME.pid"

# Screen command
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"

service_start() {
    # Check if the pid files currently exist
    if [ ! -f $STEAM/$GAME/$NAME.pid ] && [ ! -f $STEAM/$GAME/$NAME-screen.pid ]; then
        if [ -x $STEAM/$DAEMON ]; then
            echo "Starting $TITLE - $LONGNAME"
            echo "Server IP: $IP"
            echo "Server port: $PORT"
            echo "Server size: $SIZE players"
            cd $STEAM
            $INTERFACE $STEAM/$DAEMON $OPTS
            # Prevent race condition on SMP kernels
             sleep 1
            # Find and write current process id of the screen process
            ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
            echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
            echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"
            
            echo "$TITLE started."
        fi
    else
        echo -e "Cannot start $TITLE.  Server is already running."
        #exit 1
    fi
}

service_stop() {
    if [ -f $STEAM/$GAME/$NAME.pid ] && [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        echo "Stopping $TITLE - $LONGNAME."
        # Get the process ID from the pid file we created earlier
        for id in `cat $STEAM/$GAME/$NAME-screen.pid`
            do kill -9 $id
            echo "Killing process ID $id"
            echo "Removing $TITLE screen pid file"
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
        done
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        echo "$TITLE stopped."
    else
        echo -e "Cannot stop $TITLE.  Server is not running."
        #exit 1
    fi    
}    


case "$1" in
    'start')
        service_start
        ;;
    'stop')
        service_stop
        ;;
    'restart')
        service_stop
        sleep 1
        service_start
        ;;
    *)
        echo "Usage $0 start|stop|restart"
esac

On Debian, add this script to your /etc/init.d directory.
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


Please let me know if you have any questions or need help setting this up.

Cheers![/code]
Dan
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#2
Its pretty cool. You should have put in the tutorial section though i think. It is linux so who knows lol.
realchamp Wrote:
Hazz Wrote:Has someone helped you on these forums? If so, help someone else
Mooga Wrote:OrangeBox is a WHORE.
Reply
#3
@ SpartanFrog,

Good point Smile I'll add some more instructions, format it a little more nicely, and add it to the tutorials section. My second edition of the script will take a second command-line parameter to start, stop and restart multiple game servers on the same physical server.

I like scripting (as my name implies), so if anyone has requests for a script, please post here or PM me. I'd prefer a post so others can benefit.

Cheers!
Dan
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#4
Thats really cool, thanks.
Reply
#5
Hi everyone,

It's always a good idea to run any service as a user instead of root, so I'll be modifying this script to run as a user. Would it be beneficial to anyone if I wrote a tutorial on setting up a chrooted SRCDS environment? I have seen many tutorials on setting up SRCDS, but none yet on setting up SRCDS in a secure manner.

Cheers,
Dan
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#6
I´d love a walkthrough script for centos 4.2 Big Grin

I have been using screen to start stop servers, but would like a more automated manner Smile
And yes, securing the box for dummies like me would be great!
Reply
#7
scriptfu Wrote:Hi everyone,

It's always a good idea to run any service as a user instead of root, so I'll be modifying this script to run as a user. Would it be beneficial to anyone if I wrote a tutorial on setting up a chrooted SRCDS environment? I have seen many tutorials on setting up SRCDS, but none yet on setting up SRCDS in a secure manner.

Cheers,
Dan

that would be great!
Reply
#8
@Nethouse2000.gl:

I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself Toungue ).

The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.

On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.

Cheers!
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#9
scriptfu Wrote:@Nethouse2000.gl:

I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself Toungue ).

The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.

On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.

Cheers!


I use IGC game panel on my clan box and it does the job. 5 bucks a month and does everything you need to be done through a web based control. Good luck.
http://www.innovativegamingconcepts.com/
Reply
#10
VRDriver4l Wrote:
scriptfu Wrote:@Nethouse2000.gl:

I'll be putting CentOS 5.1 on my latest server. I'm a Debian fan personally, but CentOS/RedHat are both more popular. Once I get everything up and running, I'd be happy to write a tutorial for both Debian and Red Hat-based distributions. I will gladly accept requests if anyone uses something different (unless it's Gentoo, because you should be able to do it yourself Toungue ).

The CentOS tutorial should work for versions 4 and up; there is little difference with configuring chkconfig, et alia.

On another note, I have been looking for a web-based SRCDS administration toolkit, and haven't found one yet. Is there such a thing? I would like to be able to edit various files, like my server.cfg, maplist, mapcycle, etc.

Cheers!


I use IGC game panel on my clan box and it does the job. 5 bucks a month and does everything you need to be done through a web based control. Good luck.
http://www.innovativegamingconcepts.com/
Same panel I also use. Can be a bitch to setup on debian-based linux distros, but it runs well once you get the hang of it Smile.
Reply
#11
I tried to test out their demo, but it threw MySQL errors. Sad It may be worth the $5 to check it out.
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#12
tcadmin.com More proffesional but its good
realchamp Wrote:
Hazz Wrote:Has someone helped you on these forums? If so, help someone else
Mooga Wrote:OrangeBox is a WHORE.
Reply
#13
Hmm, I only host one server for now, and it's not even populated 90% of the time Toungue I'm surprised there's no open source one out there. Heck, I think I just found my next project Smile

Cheers,
Dan
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#14
Spartanfrog Wrote:tcadmin.com More proffesional but its good
Doubt he uses windows since he is coding a linux script Toungue

The only that I know is close to free (not necassarily opensource) is the cpanel one, but that's only if you already have cpanel installed on your box. http://cpgs.cpanel.net/
Reply
#15
Yeah your right lol. I know muppet was looking for beta testers for a control panel. Shoot him a PM.
realchamp Wrote:
Hazz Wrote:Has someone helped you on these forums? If so, help someone else
Mooga Wrote:OrangeBox is a WHORE.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)