12-08-2007, 06:01 PM 
	
	
	
		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.
On Debian, add this script to your /etc/init.d directory.
To start the server:
To stop the server:
To restart:
Please let me know if you have any questions or need help setting this up.
Cheers![/code]
Dan
	
	
	
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"
esacOn Debian, add this script to your /etc/init.d directory.
Code:
sudo update-rc.d <script> defaultsTo start the server:
Code:
/etc/init.d/<script> startTo stop the server:
Code:
/etc/init.d/<script> stopTo restart:
Code:
/etc/init.d/<script> restartPlease let me know if you have any questions or need help setting this up.
Cheers![/code]
Dan

 
 
 

 

![[Image: banner_560x95.png]](http://cache.www.gametracker.com/server_info/72.52.248.250:27015/banner_560x95.png)

 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'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.
 ).
 ). It may be worth the $5 to check it out.
  It may be worth the $5 to check it out.