Can't get enough SRCDS.com? Follow us on: Twitter | IRC | Steam


Post Reply 

Linux init script to start server on boot

Author Message
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #1
Linux init script to start server on boot
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]
12-08-2007 06:01 PM
Find all posts by this user Quote this message in a reply
Spartanfrog Offline
Super Moderator

Posts: 2,273
Joined: May 2007
Reputation: 11
Post: #2
RE: Linux init script to start server on boot
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.
12-09-2007 04:09 AM
Find all posts by this user Quote this message in a reply
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #3
RE: Linux init script to start server on boot
@ 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]
12-09-2007 05:02 PM
Find all posts by this user Quote this message in a reply
jameshaigh Offline
Member

Posts: 99
Joined: Jun 2007
Reputation: 0
Post: #4
RE: Linux init script to start server on boot
Thats really cool, thanks.

Freelance Web Design & Development
12-09-2007 09:44 PM
Find all posts by this user Quote this message in a reply
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #5
RE: Linux init script to start server on boot
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]
12-10-2007 08:02 PM
Find all posts by this user Quote this message in a reply
Nethouse2000.gl Offline
Junior Member

Posts: 14
Joined: Mar 2007
Reputation: 0
Post: #6
RE: Linux init script to start server on boot
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!
(This post was last modified: 12-11-2007 10:39 AM by Nethouse2000.gl.)
12-11-2007 10:36 AM
Find all posts by this user Quote this message in a reply
HBS|Ryan Offline
HotBettyServers.com

Posts: 479
Joined: Mar 2007
Reputation: 0
Post: #7
RE: Linux init script to start server on boot
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!

The Very Best in West Coast CSS
1000fps CSS Servers
http://www.HotBettyServers.com
12-11-2007 12:50 PM
Visit this user's website Find all posts by this user Quote this message in a reply
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #8
RE: Linux init script to start server on boot
@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]
12-11-2007 05:42 PM
Find all posts by this user Quote this message in a reply
VRDriver4l Offline
Balance Point

Posts: 71
Joined: Jan 2006
Reputation: 0
Post: #9
RE: Linux init script to start server on boot
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/
12-11-2007 11:08 PM
Visit this user's website Find all posts by this user Quote this message in a reply
trev-gdr Offline
Junior Member

Posts: 24
Joined: Dec 2007
Reputation: 0
Post: #10
RE: Linux init script to start server on boot
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.
12-12-2007 02:31 AM
Find all posts by this user Quote this message in a reply
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #11
RE: Linux init script to start server on boot
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]
12-12-2007 03:12 AM
Find all posts by this user Quote this message in a reply
Spartanfrog Offline
Super Moderator

Posts: 2,273
Joined: May 2007
Reputation: 11
Post: #12
RE: Linux init script to start server on boot
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.
12-12-2007 06:29 AM
Find all posts by this user Quote this message in a reply
scriptfu Offline
Junior Member

Posts: 15
Joined: Dec 2007
Reputation: 1
Post: #13
RE: Linux init script to start server on boot
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]
12-12-2007 07:50 PM
Find all posts by this user Quote this message in a reply
trev-gdr Offline
Junior Member

Posts: 24
Joined: Dec 2007
Reputation: 0
Post: #14
RE: Linux init script to start server on boot
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/
12-13-2007 04:01 AM
Find all posts by this user Quote this message in a reply
Spartanfrog Offline
Super Moderator

Posts: 2,273
Joined: May 2007
Reputation: 11
Post: #15
RE: Linux init script to start server on boot
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.
12-13-2007 06:00 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump: